Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'instance': 0.05; 'instance,': 0.05; 'exits': 0.07; 'python': 0.08; '21,': 0.09; 'subject:library': 0.09; 'url:%s': 0.09; 'pm,': 0.10; 'wrote:': 0.14; 'accordingly,': 0.16; 'daemons': 0.16; 'forks': 0.16; 'pid,': 0.16; 'simplified': 0.16; 'subject:subprocess': 0.16; 'url:localhost': 0.16; '\xc2\xa0if': 0.16; 'cc:addr:python-list': 0.17; 'skip:= 70': 0.17; 'simpler': 0.19; 'command': 0.19; 'starts': 0.20; 'work,': 0.20; 'header:In-Reply-To:1': 0.21; 'tools,': 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'libraries': 0.25; 'says': 0.27; "i'm": 0.27; 'sender:addr:gmail.com': 0.28; 'message-id:@mail.gmail.com': 0.28; 'sat,': 0.29; 'subject:how': 0.29; 'exit': 0.29; 'cc:addr:python.org': 0.30; 'second': 0.30; 'skip:% 10': 0.30; 'skip:\xc2 20': 0.30; 'it.': 0.31; 'calling': 0.31; 'named': 0.32; "i've": 0.33; 'rather': 0.34; 'done.': 0.34; 'there': 0.35; 'typical': 0.35; 'using': 0.35; '8bit%:8': 0.36; 'running': 0.37; 'similar': 0.37; 'received:google.com': 0.37; 'issue': 0.37; 'ways': 0.37; 'think': 0.38; 'received:74.125.82': 0.38; 'received:74.125': 0.38; 'could': 0.38; 'subject:from': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'skip:s 20': 0.39; 'should': 0.39; "i'd": 0.39; 'launch': 0.39; 'goes': 0.39; 'getting': 0.40; 'more': 0.60; 'your': 0.60; 'matter': 0.63; 'view': 0.66; 'here': 0.66; 'skip:\xc2 10': 0.72; 'become': 0.72; 'nice,': 0.84; 'good,': 0.91; 'running,': 0.91; 'you).': 0.95; 'to:addr:nobody': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:in-reply-to:references:from :date:x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=Kx603FoABCAmt6VDH3bcys6HX/q8q01Zv4DhwAvem2s=; b=Ns0CPNpbRkvP1Us+HrSYIsvvGf4fjzfHlo7IrlHe6SMGo018yz70PnOXBvfGW2XGXA +YuH0JbwDG9EUh8drncmBcK4qNU5k8SS2TguQ6N8dLQTvPk5wye9Znupox0uCUYe23Fr AJDBU66H8KN7kaorZSxwZPnD5fZUMP9QKigV8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; b=TNzutTwZMf6rnogI893W26kf027XqMkV88+TYr68teEytbUsZ9MUut4idg752zq5s5 SsZTDLaxq28ks0/pWdnzJAIWmoXSGx7Bzl4nM5Gd7xXqNIMOL0ygB9iV9BdgSSXqHWkT CHBskmwcosUMqO5SNrnPf2V14mYfMdtBSluc4= MIME-Version: 1.0 Sender: kushal.kumaran@gmail.com In-Reply-To: References: From: Kushal Kumaran Date: Sun, 22 May 2011 15:34:08 +0530 X-Google-Sender-Auth: 4q6k8jqwwp7Q7strYmLe7oInpVA Subject: Re: how to get PID from subprocess library To: TheSaint Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 67 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306058670 news.xs4all.nl 49041 [::ffff:82.94.164.166]:34552 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5971 On Sat, May 21, 2011 at 6:20 PM, TheSaint wrote: > Kushal Kumaran wrote: > >> That's how it is able to give you the status. =C2=A0So, if you >> are using getstatusoutput, you will have only one instance of your >> command running. > > My intent is to launch only one program instance, which will goes as daem= on. > To avoid a second call I'd like rather to use Python than > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3Dcode=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > =C2=A0 =C2=A0def start(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0'''try to start aria2c as a daemon and return = its handle to where it > can > =C2=A0 =C2=A0 =C2=A0 =C2=A0proceed to issue commands''' > > =C2=A0 =C2=A0 =C2=A0 =C2=A0# aria2c is running, then don't try it again > =C2=A0 =C2=A0 =C2=A0 =C2=A0if (chkout('ps -A |grep aria2c')[0] > 0): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0try: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0chkout(self.ARIA_C= MD) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0except: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0raise SystemExit('= aria2c is not working as deamon') > =C2=A0 =C2=A0 =C2=A0 =C2=A0elif self.handle: return self.handle > =C2=A0 =C2=A0 =C2=A0 =C2=A0# everything is good, it will return an handle > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.handle=3D \ > =C2=A0 =C2=A0 =C2=A0 =C2=A0xmlrpclib.ServerProxy('http://localhost:%s/rpc= ' %int(self.numport)) > =C2=A0 =C2=A0 =C2=A0 =C2=A0return self.handle > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3Dcode=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > Here I've named subprocess.getstatusoutput as chkout, I'm calling 2 more > programs to find whether there's a running instance of aria2c. I think it= 's > not nice, python libraries should get the matter done. > Unfortunately, because of the way daemons work, you will not be able to do this (there's some trickery with ptrace and similar tools, but surely there must be simpler ways for you). Here's a very simplified view of a typical daemon's startup: - your process (let's call it pid1) starts a program which says it will daemonize (let's call it pid2). - pid2 forks an additional process (pid3), then pid2 exits - pid1 gets the exit status of its own child (pid2) Accordingly, even if you get a PID, it will only be pid2, which is not the PID of the daemon process. You will need some other way of getting at the daemon's PID. You could look for a way to make aria2c not become a daemon and use subprocess.Popen to start it. That gives you the PID and ways to see if the process is still running. --=20 regards, kushal