Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82466
| Return-Path | <python-python-list@m.gmane.org> |
|---|---|
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'output': 0.05; '#print': 0.09; 'dan': 0.09; 'exits': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:# 30': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; "'w')": 0.16; 'finishes': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sec': 0.16; 'sooner.': 0.16; 'stdout': 0.16; 'subject:make': 0.16; 'subject:run': 0.16; 'skip:# 20': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'properly': 0.19; 'command': 0.22; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'file.': 0.24; 'script': 0.25; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'leave': 0.29; '[1]': 0.29; 'am,': 0.29; "doesn't": 0.30; 'dec': 0.30; 'robert': 0.30; '(possibly': 0.31; 'pipe': 0.31; 'writes:': 0.31; 'probably': 0.32; 'run': 0.32; 'fri,': 0.33; 'not.': 0.33; 'skip:# 10': 0.33; 'something': 0.35; 'shows': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'seconds': 0.37; 'skip:o 20': 0.38; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; '12,': 0.39; 'use.': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'signal': 0.60; 'real-world': 0.68; 'killing': 0.84; 'received:89': 0.85; 'state.': 0.95 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Akira Li <4kir4.1i@gmail.com> |
| Subject | Re: How to make subprocess run for 60 sec? |
| Date | Fri, 12 Dec 2014 23:41:02 +0300 |
| References | <CAEMktPWOmQ53Y-j-ygxSow8CjbePSH5VigXLSkwBn17DsWn4OQ@mail.gmail.com> <CAGGBd_pN9jJ5ZJ9DCVnAVZ5WQ3E1Cpc6rwOSXP91biNFHHS2dw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| X-Gmane-NNTP-Posting-Host | 89.169.229.68 |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| Cancel-Lock | sha1:+434eYdl2paEI1dGWy1nqH6Otv0= |
| 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.16926.1418416972.18130.python-list@python.org> (permalink) |
| Lines | 85 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1418416972 news.xs4all.nl 2872 [2001:888:2000:d::a6]:50241 |
| 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!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
| Xref | csiph.com comp.lang.python:82466 |
Show key headers only | View raw
Dan Stromberg <drsalists@gmail.com> writes:
> 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)
You should probably call p.wait() after p.send_signal(), to wait until
the child process exits (possibly properly flushing its stdout buffer).
It might make the output.txt content less garbled.
>
> 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.
The answer on StackOverflow [1] shows how to avoid waiting n seconds if the
subprocess finishes sooner.
[1] http://stackoverflow.com/questions/27443480/how-to-make-subprocess-run-for-60-sec
> 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.
--
Akira
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: How to make subprocess run for 60 sec? Akira Li <4kir4.1i@gmail.com> - 2014-12-12 23:41 +0300
csiph-web