sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
radim.marek@gmail.com's Avatar

FreeBSD KeyboardInterrupt not captured


Question posted by: radim.marek@gmail.com (Guest) on June 27th, 2008 05:23 PM
Hi Guys,

during testing of my project on FreeBSD I've discovered stange
'feature' of time.sleep(). It works if single thread is running, but
when multi-threaded, the SIGINT signal seems not to be handled in same
way.

I've found three discussion about similar behavior with os.system()
but so far none of the suggested patches worked.

I've limited test to following code:

import time
import sys
import threading

class MyThread(threading.Thread):
def __init__(self):
super(MyThread, self).__init__()
self.start()

def run(self):
while True:
try:
time.sleep(10)
except KeyboardInterrupt:
print 'thread keyboard interrupt'

def main():
my = MyThread()

while True:
try:
time.sleep(5)
except KeyboardInterrupt:
print 'got it'

if __name__ == '__main__':
main()


Any suggestions?

Best regards,

Radim
1 Answer Posted
Rhamphoryncus's Avatar
Rhamphoryncus June 27th, 2008 05:23 PM
Guest - n/a Posts
#2: Re: FreeBSD KeyboardInterrupt not captured

On May 7, 5:00 am, radim.ma...@gmail.com wrote:
Quote:
Originally Posted by
Hi Guys,
>
during testing of my project on FreeBSD I've discovered stange
'feature' of time.sleep(). It works if single thread is running, but
when multi-threaded, the SIGINT signal seems not to be handled in same
way.
>
I've found three discussion about similar behavior with os.system()
but so far none of the suggested patches worked.
>
I've limited test to following code:
>
import time
import sys
import threading
>
class MyThread(threading.Thread):
def __init__(self):
super(MyThread, self).__init__()
self.start()
>
def run(self):
while True:
try:
time.sleep(10)
except KeyboardInterrupt:
print 'thread keyboard interrupt'
>
def main():
my = MyThread()
>
while True:
try:
time.sleep(5)
except KeyboardInterrupt:
print 'got it'
>
if __name__ == '__main__':
main()
>
Any suggestions?


Python doesn't support interrupting non-main threads with a signal.
You have to use something else you can manually end, like a Condition
with a timeout or a poll of a fd with a timeout.
 
Not the answer you were looking for? Post your question . . .
197,042 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,042 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors