FAQ
hello,

I'm using to launch a program by subprocess.getstatusoutput. I'd like to
know whether I can get the program ID, in order to avoid another launch.

For clarity sake, I'm calling aria2 (the download manager for linux) and I
wouldn't like to call one more instance of it. So what will I use to find
the PID of the launched program?

--
goto /dev/null

Search Discussions

  • Miki Tebeka at May 19, 2011 at 4:05 pm
    The best module for doing such things is subprocess. And the Popen object has a pid attribute (http://docs.python.org/library/subprocess.html#subprocess.Popen.pid)
  • TheSaint at May 20, 2011 at 3:19 pm

    Miki Tebeka wrote:

    The best module for doing such things is subprocess. And the Popen object
    has a pid attribute
    I knew that, it's my fault that I'm not good to manage with popen. I found
    simplier to use subprocess.getstatusoutput. Maybe this function doesn't
    return the child pid, so I should adopt to work with Popen :(

    --
    goto /dev/null
  • Kushal Kumaran at May 21, 2011 at 5:16 am

    On Thu, May 19, 2011 at 9:32 PM, TheSaint wrote:
    hello,

    I'm using to launch a program by subprocess.getstatusoutput. I'd like to
    know whether I can get the program ID, in order to avoid another launch.

    For clarity sake, I'm calling aria2 (the download manager for linux) and I
    wouldn't like to call one more instance of it. So what will I use to find
    the PID of the launched program?
    The getstatusoutput function will only return when the command has
    finished. 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.

    --
    regards,
    kushal
  • TheSaint at May 21, 2011 at 12:50 pm

    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.

    --
    goto /dev/null
  • Kushal Kumaran at May 22, 2011 at 10:04 am

    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. ?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
  • TheSaint at May 22, 2011 at 1:41 pm

    Kushal Kumaran wrote:

    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
    I see. It's a step that I've to get on my account. Unfortunately I'll have
    to study it some more.

    BTW. I removed grep from the command. Python does it with the *in* statement
    :)


    --
    goto /dev/null
  • GMail Felipe at May 22, 2011 at 3:45 pm

    On 22/05/2011, at 10:41, TheSaint wrote:

    Kushal Kumaran wrote:
    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
    I see. It's a step that I've to get on my account. Unfortunately I'll have
    to study it some more.

    BTW. I removed grep from the command. Python does it with the *in* statement
    :)


    --
    goto /dev/null
    --
    http://mail.python.org/mailman/listinfo/python-list
    Hi,

    For the "ps" command, have you seen the psuti module?

    The link to it is: http://code.google.com/p/psutil/

    Regards,
    Felipe.
  • TheSaint at May 23, 2011 at 11:06 am

    GMail Felipe wrote:


    For the "ps" command, have you seen the psuti module?

    The link to it is: http://code.google.com/p/psutil/
    You gave a brand new start :)
    I bit of additional program to include into the package ;)

    --
    goto /dev/null
  • Anssi Saari at May 24, 2011 at 10:45 am

    TheSaint <nobody at nowhere.net.no> writes:

    self.handle= \
    xmlrpclib.ServerProxy('http://localhost:%s/rpc' %int(self.numport))
    Couldn't you just try to call something via this handle, like
    self.handle.aria2.getVersion()? If there's an error, then start aria2
    as a daemon and try again.
  • TheSaint at May 24, 2011 at 1:36 pm

    Anssi Saari wrote:

    Couldn't you just try to call something via this handle, like
    self.handle.aria2.getVersion()? If there's an error, then start aria2
    as a daemon and try again.
    Very good, you're right. Furthermore I should avoid to call that function
    several times. I think to join it with __init__ function
    The program on exit must tell aria2c to quit.

    --
    goto /dev/null

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 19, '11 at 4:02p
activeMay 24, '11 at 1:36p
posts11
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase