Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5971

Re: how to get PID from subprocess library

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 <kushal.kumaran@gmail.com>
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 <ir8cfj$2s4$1@speranza.aioe.org>
References <ir3eu1$kgq$1@speranza.aioe.org> <mailman.1872.1305955023.9059.python-list@python.org> <ir8cfj$2s4$1@speranza.aioe.org>
From Kushal Kumaran <kushal.kumaran+python@gmail.com>
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 <nobody@nowhere.net.no>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1912.1306058670.9059.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sat, May 21, 2011 at 6:20 PM, TheSaint <nobody@nowhere.net.no> wrote:
> Kushal Kumaran wrote:
>
>> That's how it is able to give you the status.  So, 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 daemon.
> To avoid a second call I'd like rather to use Python than
> ==============================code=========================================
>    def start(self):
>        '''try to start aria2c as a daemon and return its handle to where it
> can
>        proceed to issue commands'''
>
>        # aria2c is running, then don't try it again
>        if (chkout('ps -A |grep aria2c')[0] > 0):
>            try:
>                chkout(self.ARIA_CMD)
>            except:
>                raise SystemExit('aria2c is not working as deamon')
>        elif self.handle: return self.handle
>        # everything is good, it will return an handle
>        self.handle= \
>        xmlrpclib.ServerProxy('http://localhost:%s/rpc' %int(self.numport))
>        return self.handle
> ==============================code=========================================
>
> 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.

-- 
regards,
kushal

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to get PID from subprocess library TheSaint <nobody@nowhere.net.no> - 2011-05-20 00:02 +0800
  Re: how to get PID from subprocess library Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2011-05-21 10:46 +0530
    Re: how to get PID from subprocess library TheSaint <nobody@nowhere.net.no> - 2011-05-21 20:50 +0800
      Re: how to get PID from subprocess library Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2011-05-22 15:34 +0530
        Re: how to get PID from subprocess library TheSaint <nobody@nowhere.net.no> - 2011-05-22 21:41 +0800
          Re: how to get PID from subprocess library GMail Felipe <felipe.vinturini@gmail.com> - 2011-05-22 12:45 -0300
            Re: how to get PID from subprocess library TheSaint <nobody@nowhere.net.no> - 2011-05-23 19:06 +0800
      Re: how to get PID from subprocess library Anssi Saari <as@sci.fi> - 2011-05-24 13:45 +0300
        Re: how to get PID from subprocess library TheSaint <nobody@nowhere.net.no> - 2011-05-24 21:36 +0800

csiph-web