473,473 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

two questions about thread

hi there,

I'm new to python, and have two questions:

a. how to exit the whole process in a thread?
b. when thread doing a infinite loops, how to terminate the process?:
it seems that the follow doesn't work, in my Windows XP:
thread.start()
thread.join()

Regards

Jan 14 '06 #1
4 1130
iclinux wrote:
hi there,

I'm new to python, and have two questions:

a. how to exit the whole process in a thread?
sys.exit()

Works only if Thread.setDaemon(True) is invoked on all threads.
b. when thread doing a infinite loops, how to terminate the process?:
it seems that the follow doesn't work, in my Windows XP:
thread.start()
thread.join()


It works. You just don't understand _how_ it works. There is no (easy,
without major pain performance- and stability-wise and corner-case-free)
way to terminate a thread. If you want that, use a subprocess & e.g.
pyro as RPC mechanism.

Diez
Jan 14 '06 #2
iclinux wrote:
a. how to exit the whole process in a thread?
b. when thread doing a infinite loops, how to terminate the process?:
As others noted, the threading module offers Thread.setDaemon.
As the doc says: "The entire Python program exits when no active
non-daemon threads are left."

Python starts your program with one (non-daemon) thread which
is sometimes called the "main" thread. I suggest creating all
other threads as daemons. The process will then exit when the
main thread exits.

If some other thread needs to end the process, it does so by
telling the main thread to exit. For example, we might leave
the main thread waiting at a lock (or semaphore), and exit if
the lock is ever released.

it seems that the follow doesn't work, in my Windows XP:
thread.start()
thread.join()


Is that part of the questions above, or another issue?
--
--Bryan
Jan 14 '06 #3
The best way to do this is by using a flag or event that the child-threads
monitor each loop (or multiple times per loop if it's a long loop). If the
flag is set, they exit themselves.

The parent thread only has to set the flag to cause the children to die.

Kevin.
"Bryan Olson" <fa*********@nowhere.org> wrote in message
news:g2***************@newssvr13.news.prodigy.com. ..
iclinux wrote:
a. how to exit the whole process in a thread?
b. when thread doing a infinite loops, how to terminate the process?:


As others noted, the threading module offers Thread.setDaemon.
As the doc says: "The entire Python program exits when no active
non-daemon threads are left."

Python starts your program with one (non-daemon) thread which
is sometimes called the "main" thread. I suggest creating all
other threads as daemons. The process will then exit when the
main thread exits.

If some other thread needs to end the process, it does so by
telling the main thread to exit. For example, we might leave
the main thread waiting at a lock (or semaphore), and exit if
the lock is ever released.

> it seems that the follow doesn't work, in my Windows XP:
> thread.start()
> thread.join()


Is that part of the questions above, or another issue?
--
--Bryan

Jan 15 '06 #4
Kevin wrote:
The best way to do this is by using a flag or event that the child-threads
monitor each loop (or multiple times per loop if it's a long loop). If the
flag is set, they exit themselves.

The parent thread only has to set the flag to cause the children to die.


Doesn't work, because threads can be blocked. Worse,
some threads may be blocked waiting for others to release
them. The unblocked threads check the flag and exit, so
they're never signal the blocked ones.
--
--Bryan
Jan 16 '06 #5

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

Similar topics

1
by: Carlos Kirkconnell | last post by:
I'm programming a multi - threaded application using C#. I have two questions regarding to the use of threads 1- I have a Hashtable that will have multiple writters and multiple readers. I used...
2
by: Frank Rizzo | last post by:
I tried it on dotnet.vb, but got no answers. I'll try it here now. I have an app that listen to data on a certain resource. Data can come in on this resource any time, so I have to listen for it...
12
by: Keith Langer | last post by:
I have some questions about whether synclock is necessary in a few different scenarios: 1) I have a Queue class which is shared between two threads. Thread 1 pushes objects onto the queue and...
2
by: Frank Rizzo | last post by:
I have an app that listen to data on a certain resource.&nbsp; Data can come in on this resource any time, so I have to listen for it the entire time.&nbsp; I wanted to see if what I have is...
0
by: Frank Rizzo | last post by:
I have an app that listen to data on a certain resource. Data can come in on this resource any time, so I have to listen for it the entire time. I wanted to see if what I have is thread-safe and...
9
by: ljlevend | last post by:
I have two questions related to FileStreams. 1. Is there any way to determine whether a file has the permissions that are required by a FileStream constructor? For example, given the following...
27
by: Ritesh Raj Sarraf | last post by:
Hi, I have some basic doubts about thread. I have a list which has items in it which need to be downloaded from the internet. Let's say list is: list_items which has 100 items in it.
1
by: richard.hallgren | last post by:
Hi, I have some questions concerning thread pools. I've marked the questions by number in the text. First I'd like to know when which thread pools an application uses. I know ASP.NET uses the...
3
by: JJ | last post by:
I've done a little multi-threading on winform apps some time ago now, so I'm not a complete beginner, but best assume I am for any explanations..: This is an asp.net 2.0 website, using c#: I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
1
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.