473,382 Members | 1,689 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,382 software developers and data experts.

Stopping a thread from another one

Hello Newsgroup!

In my Python script, I use the 'thread' module (not 'threading') and
'signal' simultaneously. All spawned threads execute
'pcapObject.loop(-1, callback)', which does not return.

The problem now is:
When the script catch a signal (let's say: SIGHUP), only the main thread
is affected. But I need also the subthreads to be ended, because the
script reopen()s files on SIGHUP and would also re-create the threads.

NOT a solution is:
The main thread sets a lock in the signal handler, the pcap-callback()
function examines this lock everytime it's called, and if set, exit()s
the thread. This is not very beautiful.
I would prefer, if I end the subthreads by the main thread.
Is there any chance?

Greetings,
fps
Jul 18 '05 #1
6 7015
Fabiano Sidler wrote:
Hello Newsgroup!

In my Python script, I use the 'thread' module (not 'threading') and
'signal' simultaneously. All spawned threads execute
'pcapObject.loop(-1, callback)', which does not return.

The problem now is:
When the script catch a signal (let's say: SIGHUP), only the main thread
is affected. But I need also the subthreads to be ended, because the
script reopen()s files on SIGHUP and would also re-create the threads.

NOT a solution is:
The main thread sets a lock in the signal handler, the pcap-callback()
function examines this lock everytime it's called, and if set, exit()s
the thread. This is not very beautiful.
I would prefer, if I end the subthreads by the main thread.
Is there any chance?

Greetings,
fps


Incorporating a flag into each thread, which is checked periodically by
the thread to decide whether or not it should end, is the safest way to
terminate threads. However, if you're looking for a preemptive method
that kills a thread unconditionally (whether the thread wants to die or
not) then search this group for 'Kthread', a module created by Connelly
Barnes, which implements a subclass of Thread incorporating this
feature. Note that killing threads preemptively is unsafe and should
only be done with care. This is why it's not part of Python's standard
threading library.
Jul 18 '05 #2
Fabiano Sidler wrote:
I would prefer, if I end the subthreads by the main thread.
Is there any chance?


The usual idiom is something like this:

class StoppableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._running = False
def stop(self):
self._running = False
def run(self):
while self._running:
# do stuff here that loops periodically to allow
# the flag to be checked
Then from the main thread you can keep track of the child
threads and do xxx.stop() on each of them, then an optional
threading.Thread.join() call (check the docs for that).

This can be made more sophisticated as required.

-Peter
Jul 18 '05 #3
Op 2004-06-23, Chris S. schreef <ch*****@NOSPAMudel.edu>:
Fabiano Sidler wrote:
Hello Newsgroup!

In my Python script, I use the 'thread' module (not 'threading') and
'signal' simultaneously. All spawned threads execute
'pcapObject.loop(-1, callback)', which does not return.

The problem now is:
When the script catch a signal (let's say: SIGHUP), only the main thread
is affected. But I need also the subthreads to be ended, because the
script reopen()s files on SIGHUP and would also re-create the threads.

NOT a solution is:
The main thread sets a lock in the signal handler, the pcap-callback()
function examines this lock everytime it's called, and if set, exit()s
the thread. This is not very beautiful.
I would prefer, if I end the subthreads by the main thread.
Is there any chance?

Greetings,
fps


Incorporating a flag into each thread, which is checked periodically by
the thread to decide whether or not it should end, is the safest way to
terminate threads. However, if you're looking for a preemptive method
that kills a thread unconditionally (whether the thread wants to die or
not) then search this group for 'Kthread', a module created by Connelly
Barnes, which implements a subclass of Thread incorporating this
feature. Note that killing threads preemptively is unsafe and should
only be done with care. This is why it's not part of Python's standard
threading library.


I thought Python was a language for consenting adults.

--
Antoon Pardon
Jul 18 '05 #4
Antoon Pardon wrote:
Op 2004-06-23, Chris S. schreef <ch*****@NOSPAMudel.edu>:
Incorporating a flag into each thread, which is checked periodically by
the thread to decide whether or not it should end, is the safest way to
terminate threads. However, if you're looking for a preemptive method
that kills a thread unconditionally (whether the thread wants to die or
not) then search this group for 'Kthread', a module created by Connelly
Barnes, which implements a subclass of Thread incorporating this
feature. Note that killing threads preemptively is unsafe and should
only be done with care. This is why it's not part of Python's standard
threading library.


I thought Python was a language for consenting adults.


The "consulting adults" refrain comes from the idea that two people
should be allowed to engage in whatever behaviour they want,
together, if it's not harming anyone including themselves. It's
a social thing, you know, keep the government out of the bedroom.

Threads are too difficult, and multithreaded issues too little
understood by most people, to expect that people will not shoot
themselves in the foot with a thread.kill(). Newbies will see
that method and find all manner of creative ways to use it, none
of which will be either required or reliable.

Killing threads is an STD, and in this area Python makes you wear
a condom.

-Peter
Jul 18 '05 #5
Peter Hansen wrote:
Threads are too difficult, and multithreaded issues too little
understood by most people, to expect that people will not shoot
themselves in the foot with a thread.kill(). Newbies will see
that method and find all manner of creative ways to use it, none
of which will be either required or reliable.

Killing threads is an STD, and in this area Python makes you wear
a condom.


If I write the code for the threads then what exactly is the
problem?

As far as I can tell, implementing thread killing is something that
is difficult to implement in the interpretter, but rather than just
admit that, we get the "who will think of the newbies" cries instead.

Anybody can use os.remove. Do we prevent access to that? Heck,
I can subclass and not call parent constructors. Does Python
even bat an eyelid. I can open zillions of sockets and dump
spam down them. Not a peep from Python.

I can make multiple threads of execution and put in all
sorts of deadlocks. I can even write infinite loops trivially.
No sound from Python.

We already have signals and keyboardinterrupt in the main
thread and the world hasn't collapsed. I don't exactly see
how it will suddenly collapse if the same can be done to
other threads.

Roger
Jul 18 '05 #6
Op 2004-06-23, Peter Hansen schreef <pe***@engcorp.com>:
Antoon Pardon wrote:
Op 2004-06-23, Chris S. schreef <ch*****@NOSPAMudel.edu>:
Incorporating a flag into each thread, which is checked periodically by
the thread to decide whether or not it should end, is the safest way to
terminate threads. However, if you're looking for a preemptive method
that kills a thread unconditionally (whether the thread wants to die or
not) then search this group for 'Kthread', a module created by Connelly
Barnes, which implements a subclass of Thread incorporating this
feature. Note that killing threads preemptively is unsafe and should
only be done with care. This is why it's not part of Python's standard
threading library.


I thought Python was a language for consenting adults.


The "consulting adults" refrain comes from the idea that two people
should be allowed to engage in whatever behaviour they want,
together, if it's not harming anyone including themselves. It's
a social thing, you know, keep the government out of the bedroom.

Threads are too difficult, and multithreaded issues too little
understood by most people, to expect that people will not shoot
themselves in the foot with a thread.kill(). Newbies will see
that method and find all manner of creative ways to use it, none
of which will be either required or reliable.

Killing threads is an STD, and in this area Python makes you wear
a condom.


And who decides that?

Whenever someone comes up with an idea that will help prevent
people from shooting themselves in the foot, I hear the:
"Python is for consenting adults" mantra, don't take away
the flexibility of the language. Now suddenly the situation
is reversed and people ask for more flexibility and suddenly
the extra flexibility is like as STD and the consenting
adults idea not so strong as originally presented.

My impression with all that Python Zen is that people
just use it to resist change. Explicit is better than
implicit except where python currently already does things
implicitly. Python is for consenting adults except
where python has already limitations because those
limitations are for preventing STD.

--
Antoon Pardon
Jul 18 '05 #7

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

Similar topics

4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
1
by: traviscook88 | last post by:
I have successfully started a new thread that is performing a long task. I am using C# and am fairly new to it and am wondering, where do I store a reference to that running thread? I want to...
2
by: matteo | last post by:
Hi everyboby, i wrote a c# service that every XXX minute launch a working thread on timer events, something like this: private void timer_Elapsed ( object status ) { // Worker thread...
11
by: Steve | last post by:
I'm having a problem with my Thread usage and I think the general design of how I'm working with them. My UI class calls a method in another class that does a lot of work. That "worker" class...
4
by: bjm | last post by:
I am writing a program that will automate a series of application installations. I want to give the user the option of stopping the program's execution in between installations (for example, give...
10
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on...
4
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and...
2
by: Steve | last post by:
Hi All, I've been trying to come up with a good way to run a certain process at a timed interval (say every 5 mins) using the SLEEP command and a semaphore flag. The basic thread loop was always...
1
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread.Since my whole project is launched by windows service and that service will start automatically at the start of the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.