Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83314
| References | <CACT3xuUzuMH1CF3AsakdGMwa3U3BX3N+nA1ZVy4NhVmnA9-6gw@mail.gmail.com> <CACT3xuWmGF7OpoTKmEHEwzzsZNPXECn-rDKVnDtUo8-p1B1fww@mail.gmail.com> |
|---|---|
| Date | 2015-01-08 13:24 +1100 |
| Subject | Re: Playing with threads |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17461.1420683901.18130.python-list@python.org> (permalink) |
On Thu, Jan 8, 2015 at 1:00 PM, Ganesh Pal <ganesh1pal@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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Playing with threads Chris Angelico <rosuav@gmail.com> - 2015-01-08 13:24 +1100
csiph-web