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


Groups > comp.lang.python > #82460

Re: How to make subprocess run for 60 sec?

Return-Path <drsalists@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'output': 0.05; '#print': 0.09; 'exits': 0.09; 'skip:# 30': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; "'w')": 0.16; 'sec': 0.16; 'subject:make': 0.16; 'subject:run': 0.16; 'skip:# 20': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'command': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'file.': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; 'am,': 0.29; "doesn't": 0.30; 'dec': 0.30; 'robert': 0.30; 'message- id:@mail.gmail.com': 0.30; 'pipe': 0.31; 'run': 0.32; 'fri,': 0.33; 'not.': 0.33; 'skip:# 10': 0.33; 'something': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'seconds': 0.37; 'skip:o 20': 0.38; 'does': 0.39; '12,': 0.39; 'use.': 0.39; 'skip:p 20': 0.39; 'signal': 0.60; 'to:addr:gmail.com': 0.65; 'real-world': 0.68; 'killing': 0.84; 'state.': 0.95
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:to :cc:content-type; bh=AC/TTY7Gf5kkHMiD2r/zbWcSoRGooF7JhhAvH8cHga8=; b=wNtfEcsJcMdpcBHFggwZHnPl6f4ww7HsSDDOJUDUU4HDsZu0L9PPcLQTIlzCymMBuO zFiReeGTDEGwU5gw/KBnGpYrhgsnyC79OgyRNXLV4CQmwP1u16CcrciwUKrRWDOyknj8 rtIYyqgmghPtNjONUjSjC3a1ZS0yAzS01VsbDDcSVwMIizJ08rmoKGpI/qyvAmetX2ug dHSPVnovv8r/OV/4Z5RwvQJy0C7vf3Xfov9tCBN1b88ozuHmNgLH7H/Py9zfJXghT+eZ Rlj1FSjFz2UtaV0gDD0Xqu9ML9rmNAEBu+WaZsGmng0SanYB/bOm9ITT7aISCtrWMlNS Q4Xg==
MIME-Version 1.0
X-Received by 10.112.158.40 with SMTP id wr8mr16673397lbb.51.1418406105729; Fri, 12 Dec 2014 09:41:45 -0800 (PST)
In-Reply-To <CAEMktPWOmQ53Y-j-ygxSow8CjbePSH5VigXLSkwBn17DsWn4OQ@mail.gmail.com>
References <CAEMktPWOmQ53Y-j-ygxSow8CjbePSH5VigXLSkwBn17DsWn4OQ@mail.gmail.com>
Date Fri, 12 Dec 2014 09:41:45 -0800
Subject Re: How to make subprocess run for 60 sec?
From Dan Stromberg <drsalists@gmail.com>
To Robert Clove <cloverobert@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16921.1418406107.18130.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1418406107 news.xs4all.nl 2966 [2001:888:2000:d::a6]:34304
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:82460

Show key headers only | View raw


On Fri, Dec 12, 2014 at 12:48 AM, Robert Clove <cloverobert@gmail.com> wrote:
> Hi All,
>
> I have the following python script that runs.
> I want is to run the subprocess to run for 60 sec and then send the SIGINT
> signal to subprocess and write the output in file.
>
> #!/usr/bin/python
> import os
> import subprocess
> PIPE = subprocess.PIPE
> import signal
> import time
>
> def handler(signum, frame):
>     pass
>
> signal.signal(signal.SIGALRM, handler)
> signal.alarm(60)
> command = "strace -c ./server"
> os.chdir("/root/Desktop/")
> p = subprocess.Popen(command, stdout=PIPE, stderr=PIPE)
> time.sleep(60)
> p.send_signal(signal.SIGINT)
> signal.alarm(0)
> print p.communicate()[1]

Perhaps try something like:

#!/usr/bin/python

#import os
import subprocess
PIPE = subprocess.PIPE
import signal
import time

#def handler(signum, frame):
#    pass

with open('output.txt', 'w') as out_file:
    #signal.signal(signal.SIGALRM, handler)
    #signal.alarm(60)
    #command = "strace -c ./server"
    command = "./test-script"
    #os.chdir("/root/Desktop/")
    p = subprocess.Popen(command, stdout=out_file, stderr=out_file, shell=True)
    time.sleep(5)
    p.send_signal(signal.SIGINT)
    #signal.alarm(0)
    #print p.communicate()[1]

with open('output.txt', 'r') as in_file:
    output = in_file.read()

print(output)


BTW, killing an active strace may leave strace's subprocess stuck in
an unkillable state.

Also note that this will sleep for n seconds whether the subprocess
takes that long or not.

If you just want a program that does this, and it doesn't have to be
in Python, you might try
http://stromberg.dnsalias.org/~strombrg/maxtime.html
It's in C, and is the product of considerable real-world use.  It
exits almost immediately after its subprocess exits, FWIW.

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


Thread

Re: How to make subprocess run for 60 sec? Dan Stromberg <drsalists@gmail.com> - 2014-12-12 09:41 -0800

csiph-web