Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail From: Grant Edwards Newsgroups: comp.lang.python Subject: Re: Feature Request: Reposition Execution Date: Mon, 11 May 2015 14:27:54 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 67 Message-ID: References: <2d482$5550967d$5419aafe$27910@news.ziggo.nl> <5550a223$0$13013$c3e8da3$5496439d@news.astraweb.com> NNTP-Posting-Host: 67-130-15-94.dia.static.qwest.net X-Trace: reader1.panix.com 1431354474 27739 67.130.15.94 (11 May 2015 14:27:54 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 11 May 2015 14:27:54 +0000 (UTC) User-Agent: slrn/1.0.1 (Linux) Xref: csiph.com comp.lang.python:90385 On 2015-05-11, Steven D'Aprano wrote: > On Mon, 11 May 2015 09:57 pm, Dave Angel wrote: > >> On 05/11/2015 07:46 AM, Skybuck Flying wrote: >>> Hello, >>> >>> Sometimes it can be handy to "interrupt/reset/reposition" a running >>> script. >>> >>> For example something externally goes badly wrong. >>> >> >> os.kill() >> >> then in your process, handle the exception, and do whatever you think is >> worthwhile. > > > Are you suggesting that the app sends itself a signal? > > Is that even allowed? Of course (at least on Unix/Linux/Posix systems). And there's even a special case defined to make sending signals to yourself easy: you just send them to PID 0. From "man 2 kill" on Linux: DESCRIPTION The kill() system call can be used to send any signal to any process group or process. [...] If pid equals 0, then sig is sent to every process in the process group of the calling process. And just to make sure I ran a little test, and it works exactly as advertised: ---------------------------------testit.py-------------------------------- #!/usr/bin/python import os, sys, time, threading, signal def thread1(): while True: sys.stdout.write("Hello %s\n" % time.time()) time.sleep(1) threading.Thread(target=thread1).start() time.sleep(2) os.kill(0,signal.SIGKILL) --------------------------------------------------------------------------- $ ./testit.py Hello 1431354383.19 Hello 1431354384.19 Killed $ -- Grant Edwards grant.b.edwards Yow! Hello. Just walk at along and try NOT to think gmail.com about your INTESTINES being almost FORTY YARDS LONG!!