Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'string': 0.09; 'exit': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'jan': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'grep': 0.16; 'stdout': 0.16; 'subject:threads': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'thu,': 0.19; 'command': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'lines': 0.31; 'pipe': 0.31; 'url:python': 0.33; 'running': 0.33; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'skip:s 60': 0.36; 'done': 0.36; 'url:org': 0.36; 'step': 0.37; 'url:library': 0.38; 'pm,': 0.38; 'flow': 0.39; 'read': 0.60; 'monitoring': 0.61; 'url:3': 0.61; 'success': 0.61; "you'll": 0.62; 'receive': 0.70; '1:00': 0.84; '2015': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=T/e7ppbsgT/qGNDKLzAkfqExkOW56OnbX8V9BBWW8C4=; b=XWy9ODb7zZbcQ1dbWZU0mW9MNMISmvQDc9Ml5HPPScpOArO+BM2iG7IHCfHar6uijx WeYq7c5A871oVDCois3augcl/rAXubrDYzzhvMoUHKeyClct4PTEm9DAT//sMPfgOy9k qDbQVC+ZupQWU2XBl77dQoTi5f1JZZg0fnuMgcSpy1nX7wJXJtYBQf7dXuEpylIkoN+M pzi0lcvh/hwX2S6gkfLVWwAJ5PpW8EDsfy6FhK2USNWma7Xvn5lxMJRRedUdIb3/9VIJ rbmWAeJ3qFJfBw/RHJ7vzUa7OR/4fBuXcPV8qXOA06dfblQqlB+ycxSN2R+jB9JtI5if jmvQ== MIME-Version: 1.0 X-Received: by 10.50.62.104 with SMTP id x8mr25730895igr.2.1420683892306; Wed, 07 Jan 2015 18:24:52 -0800 (PST) In-Reply-To: References: Date: Thu, 8 Jan 2015 13:24:52 +1100 Subject: Re: Playing with threads From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420683901 news.xs4all.nl 2901 [2001:888:2000:d::a6]:59000 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83314 On Thu, Jan 8, 2015 at 1:00 PM, Ganesh Pal 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