473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

threads and sys.exit()

calling sys.exit() from a thread does nothing... the thread dies, but
the interpreter remains. i guess the interpreter just catches and
ignore the SystemExit exception...

does anybody know of a way to overcome this limitation?
-tomer

Apr 24 '06 #1
9 23405
gangesmaster wrote:
calling sys.exit() from a thread does nothing... the thread dies, but
the interpreter remains. i guess the interpreter just catches and
ignore the SystemExit exception...

does anybody know of a way to overcome this limitation?


Use Thread.setDaemon(True) on your threads.

diez
Apr 24 '06 #2
>>> import threading
t=threading.Thread(target=sys.exit)
t.setDaemon(True)
t.start()


?

Apr 24 '06 #3
(i forgot to say it didn't work)

Apr 24 '06 #4
gangesmaster:
(i forgot to say it didn't work)


It's the remaining threads that need to be daemon, when some thread
performs sys.exit. When no non-daemon thread remains, the application
terminates.

--
René Pijlman
Apr 24 '06 #5
gangesmaster wrote:
calling sys.exit() from a thread does nothing... the thread dies, but
the interpreter remains. i guess the interpreter just catches and
ignore the SystemExit exception...

does anybody know of a way to overcome this limitation?

call thread.interrupt_main()

on *NIX: os.kill(os.getpid(),signal.XXXX)

or best design your threading correctly with resonable/flexible
communication channels, e.g. CallQueue:
http://aspn.activestate.com/ASPN/Coo.../Recipe/491281

(infact that latter (example2) uses sys.exit/SystemExit the other way
correctly: to terminate a thread cleanly in functional style. in the
same style reverse the mainthread could be terminated cleanly.)

I'd

-robert

Apr 24 '06 #6
i can't make the main thread daemonic. the situation is this:
* the main thread starts a thread
* the new thread does sys.exit()
* the new thread dies, but the process remains
i can do os.kill(os.getpid()), or TerminateProcess(-1) but that's not
what i want
-tomer

Apr 24 '06 #7
that's not a question of design. i just want a child-thread to kill the
process. in a platform agnostic way.

Apr 24 '06 #8
gangesmaster wrote:
that's not a question of design. i just want a child-thread to kill the
process. in a platform agnostic way.


the clean kill is the 'KeyboardInterrupt'. your code finalizations are
at least done corretly. thats also raised on SIGINT by default.

so thread.interrupt_main() is that thing to go for on this level - lets
say on mid level.

killing hard (SIGKILL etc ) is low level. it should be OS-specific, as
Python should not make itself inconsistent

a good consistent killing on high level can only be done by inter-thread
communication - as shown in this recipe above. even if a
KeyboardInterrupt is thrown, like with ..

#raises SystemExit inside my_thread
cq.call(sys.exit, wait=1, raise_exception=2)

... its thrown exactly app-deterministic a callqueue.receive() time.

so you have all levels and tools available. If you really miss
something, then I'd still say, the design question should be raised.

-robert

PS: SystemExit / sys.exit (doc: "Exit from Python"!?) is
misleading/historic. Its in fact almost "ThreadExit".

Apr 24 '06 #9
robert wrote:
killing hard (SIGKILL etc ) is low level. it should be OS-specific, as
Python should not make itself inconsistent


(and os._exit() is that at mid-low level ; better not to say; no
finalizations etc.)

-robert
Apr 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Maxwell Hammer | last post by:
An application I am developing executes many threads and then has a "monitor" part that waits for certain events. One of these events causes the application to have to shutdown. On shutdown the...
7
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
4
by: MSDousti | last post by:
Hi I have written a VB .NET app, which uses several threads. I thought that when the user closes the main window (when MainForm.closed event occures, and I call application.exit) all running...
3
by: Keith Mills | last post by:
Hello, please find attached a basic outline of what I am attempting to accomplish... basically I want to create a number of THREADS (which I can do fine), but I then need a method for them to be...
0
by: Lloyd Zusman | last post by:
I have a python-2.5 program running under linux in which I spawn a number of threads. The main thread does nothing while these subsidiary threads are running, and after they all complete, the main...
4
by: Marcus Alves Grando | last post by:
Hello list, I have a strange problem with os.walk and threads in python script. I have one script that create some threads and consume Queue. For every value in Queue this script run os.walk()...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
7
by: Brendon Costa | last post by:
Hi all, I have a small python project i am working on. Basically i always have two threads. A "Read" thread that sits in a loop reading a line at a time from some input (Usually stdin) and then...
7
by: matthewaveryusa | last post by:
I have been reading a lot about aborting threads and there seems to be something missing to do this easily on program exit. This is what I want to do but I will need some help. I declared a global...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.