By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

Re: threading

P: n/a
2008/8/13 Parimala <pa********@accord-soft.com>:
Hello,

I am using python2.5.1 version to run my test scripts. I want to use
'threading' module in my tests. As a startup program, I had run the
following one.

import threading
import sys
import time

def hello():
i=0
try:
while i<10:
print "hi"
time.sleep(1)
i+=1
except KeyboardInterrupt:
print 'KeyboardInterrupt'
raise KeyboardInterrupt

try:
thread=threading.Thread(target=hello,args=())
thread.start()
except KeyboardInterrupt:
print 'KeyboardInterrupt'
raise KeyboardInterrupt

once program starts, problem is..
I am not able to abort the thread using (CTRL+C) KeyboardInterrupt. While
running if I press CTRL+C, it won't generate any exception until the end of
the execution. Once the execution gets over, it will give "Exception
exceptions.KeyboardInterrupt in <module 'threading' from
'C:\python25\lib\threading.py'ignored" this message and exits.

I had gone through some documents, it says if a thread is joined with
.join() method then we can't stop that process until it releases the lock
what it acquired. But in the above program I didn't use .join() method but
still I am not able to abort the thread.

Could you please suggest me how can I abort the thread at any point in time
using CTRL+C.
Hi,

a terminate method is given here:
http://sebulba.wikispaces.com/recipe+thread2

so you can terminate the thread by:

(...)
t.start()
(...)

while True:
try:
#some code
except KeyboardInterrupt:
t.terminate()
break

Mathieu
Aug 14 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Aug 14, 3:30*am, "Mathieu Prevot" <mathieu.pre...@ens.frwrote:
2008/8/13 Parimala <parimal...@accord-soft.com>:
Hello,
* * * *I am using python2.5.1 version to run my test scripts. Iwant to use
'threading' module in my tests. As a startup program, I had run the
following one.
importthreading
import sys
import time
def hello():
* i=0
* try:
* * while i<10:
* * * print "hi"
* * * time.sleep(1)
* * * i+=1
* except KeyboardInterrupt:
* * print 'KeyboardInterrupt'
* * raise KeyboardInterrupt
try:
* thread=threading.Thread(target=hello,args=())
* thread.start()
except KeyboardInterrupt:
* print 'KeyboardInterrupt'
* raise KeyboardInterrupt
once program starts, problem is..
I am not able to abort the thread using (CTRL+C) KeyboardInterrupt. *While
running if I press CTRL+C, it won't generate any exception until the end of
the execution. Once the execution gets over, *it will give "Exception
exceptions.KeyboardInterrupt in <module 'threading' from
'C:\python25\lib\threading.py'ignored" this message and exits.
I had gone through some documents, it says if a thread is joined with
.join() method then we can't stop that process until it releases the lock
what it acquired. But in the above program I didn't use .join() method but
still I am not able to abort the thread.
Could you please suggest me how can I abort the thread at any point in time
using CTRL+C.

Hi,

a terminate method is given here:http://sebulba.wikispaces.com/recipe+thread2

so you can terminate the thread by:

(...)
t.start()
(...)

while True:
* try:
* * #some code
* except KeyboardInterrupt:
* * t.terminate()
* * break

Mathieu
(For some reason the OP hasn't come through to groups.google.com..
odd)

Note that it won't interrupt any blocked I/O, which is often what you
need the most. If your threads are CPU-bound you can simply have them
check a flag and exit if it becomes True.
Aug 14 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.