Playing with threads
Chris Angelico
rosuav at gmail.com
Wed Jan 7 21:24:52 EST 2015
On Thu, Jan 8, 2015 at 1:00 PM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
> I'm trying to use threads to achieve the below work flow
>
> 1. Start a process , while its running grep for a string 1
> 2. Apply the string 1 to the command in step 1 and exit step 2
> 3. Monitor the stdout of step1 and print success if the is pattern found
If by "grep" you mean "search", then this would be best done by
spawning a subprocess and monitoring its output.
https://docs.python.org/3/library/subprocess.html
Set stdout=PIPE, then read from the stdout member, and you'll be
reading the program's output. Something like this:
with subprocess.Popen(["find","/","-name","*.py"],stdout=subprocess.PIPE)
as findpy:
for line in findpy.stdout:
# do something with line
You'll get lines as they're produced, and can use standard string
manipulation on them. I don't know what your step 2 is, but you can
set stdin to be a pipe as well, and then you just write to your end
and the process will receive that on its standard input.
No threads needed; just your Python process and the subprocess.
ChrisA
More information about the Python-list
mailing list