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

killing thread after timeout

Hello.

I am going to write python script which will read python command from
socket, run it and return some values back to socket.

My problem is, that I need some timeout. I need to say for example:

os.system("someapplication.exe")

and kill it, if it waits longer than let's say 100 seconds

I want to call command on separate thread, then after given timeout -
kill thread, but I realized (after reading Usenet archive) that there is
no way to kill a thread in Python.

How can I implement my script then?

PS. it should be portable - Linux, Windows, QNX, etc
Sep 6 '05 #1
10 9834
After reading more archive I think that solution may be to raise an
Exception after timeout, but how to do it portable?
Sep 6 '05 #2
You'll need a watchdog thread that handles the other threads. The
watchdog thread just builds a table of when threads were started, and
after a certain # of seconds, expires those threads and kills them.

Sep 6 '05 #3
Jacek Popławski wrote:
Hello.

I am going to write python script which will read python command from
socket, run it and return some values back to socket.

My problem is, that I need some timeout. I need to say for example:

os.system("someapplication.exe")

and kill it, if it waits longer than let's say 100 seconds

I want to call command on separate thread, then after given timeout -
kill thread, but I realized (after reading Usenet archive) that there is
no way to kill a thread in Python.

How can I implement my script then?

PS. it should be portable - Linux, Windows, QNX, etc

Probably the easiest way is to use select with a timeout (see the
docs for library module select). eg.

a, b c = select.select([mySocket], [], [], timeout)
if len(a) == 0:
print 'We timed out'
else:
print 'the socket has something for us'
Steve
Sep 6 '05 #4
Jacek Popławski wrote:
I am going to write python script which will read python command from
socket, run it and return some values back to socket.
(Sounds like a huge security risk, unless you have tight control over
who can connect to that socket.)
My problem is, that I need some timeout. I need to say for example:

os.system("someapplication.exe")

and kill it, if it waits longer than let's say 100 seconds

I want to call command on separate thread, then after given timeout -
kill thread, but I realized (after reading Usenet archive) that there is
no way to kill a thread in Python.


The issue isn't killing a thread in Python, it's killing the *new
process* which that thread has started. To do that you have to rely on
OS-specific (i.e. non-portable) techniques. Googling for "python kill
process" would probably get you off to a good start.

-Peter
Sep 6 '05 #5

Jacek Poplawski had written:
I am going to write python script which will read python
command from socket, run it and return some values back to
socket.

My problem is, that I need some timeout.

Jacek Poplawski wrote: After reading more archive I think that solution may be to raise an
Exception after timeout, but how to do it portable?


Python allows any thread to raise a KeyboardInterrupt in the
main thread (see thread.interrupt_main), but I don't think there
is any standard facility to raise an exception in any other
thread. I also believe, and hope, there is no support for lower-
level killing of threads; doing so is almost always a bad idea.
At arbitrary kill-times, threads may have important business
left to do, such as releasing locks, closing files, and other
kinds of clean-up.

Processes look like a better choice than threads here. Any
decent operating system will put a deceased process's affairs
in order.
Anticipating the next issues: we need to spawn and connect to
the various worker processes, and we need to time-out those
processes.

First, a portable worker-process timeout: In the child process,
create a worker daemon thread, and let the main thread wait
until either the worker signals that it is done, or the timeout
duration expires. As the Python Library Reference states in
section 7.5.6:

A thread can be flagged as a "daemon thread". The
significance of this flag is that the entire Python program
exits when only daemon threads are left.

The following code outlines the technique:

import threading

work_is_done = threading.Event()

def work_to_do(*args):
# ... Do the work.
work_is_done.set()

if __name__ == '__main__':
# ... Set stuff up.
worker_thread = threading.Thread(
target = work_to_do,
args = whatever_params)
worker_thread.setDaemon(True)
worker_thread.start()
work_is_done.wait(timeout_duration)

Next, how do we connect the clients to the worker processes?

If Unix-only is acceptable, we can set up the accepting socket,
and then fork(). The child processes can accept() incomming
connections on its copy of the socket. Be aware that select() on
the process-shared socket is tricky, in that that the socket can
select as readable, but the accept() can block because some
other processes took the connection.
If we need to run on Windows (and Unix), we can have one main
process handle the socket connections, and pipe the data to and
from worker processes. See the popen2 module in the Python
Standard Library.
--
--Bryan
Sep 6 '05 #6
Bryan Olson wrote:
[Some stuff he thinks is right, but might not answer the real
question]

Definitely look into Peter Hanson's answer.

Olson's answer was about timing-out one's own Python code.
Bryan Olson has heretofore avoided referring to himself in the
third person, and will hence forth endeavor to return to his
previous ways.

--
--Bryan
Sep 6 '05 #7
Bryan Olson wrote:
First, a portable worker-process timeout: In the child process,
create a worker daemon thread, and let the main thread wait
until either the worker signals that it is done, or the timeout
duration expires.
It works on QNX, thanks a lot, your reply was very helpful!
If we need to run on Windows (and Unix), we can have one main
process handle the socket connections, and pipe the data to and
from worker processes. See the popen2 module in the Python
Standard Library.


popen will not work in thread on QNX/Windows, same problem with spawnl
currently I am using:

os.system(command+">file 2>file2")

it works, I just need to finish implementing everything and check how it
may fail...

One more time - thanks for great idea!

Sep 8 '05 #8
Bryan Olson <fa*********@nowhere.org> writes:
First, a portable worker-process timeout: In the child process,
create a worker daemon thread, and let the main thread wait
until either the worker signals that it is done, or the timeout
duration expires. As the Python Library Reference states in
section 7.5.6:
Maybe the child process can just use sigalarm instead of a separate
thread, to implement the timeout.
If Unix-only is acceptable, we can set up the accepting socket,
and then fork(). The child processes can accept() incomming
connections on its copy of the socket. Be aware that select() on
the process-shared socket is tricky, in that that the socket can
select as readable, but the accept() can block because some
other processes took the connection.


To get even more OS-specific, AF_UNIX sockets (at least on Linux) have
a feature called ancillary messages that allow passing file
descriptors between processes. It's currently not supported by the
Python socket lib, but one of these days... . But I don't think
Windows has anything like it. No idea about QNX.
Sep 8 '05 #9
Paul Rubin wrote:
Maybe the child process can just use sigalarm instead of a separate
thread, to implement the timeout.
Already tried that, signals works only in main thread.
To get even more OS-specific, AF_UNIX sockets (at least on Linux) have
a feature called ancillary messages that allow passing file
descriptors between processes. It's currently not supported by the
Python socket lib, but one of these days... . But I don't think
Windows has anything like it. No idea about QNX.


I have solved problem with additional process, just like Bryan Olson
proposed. Looks like all features I wanted are working... :)
Sep 8 '05 #10
Paul Rubin wrote:
To get even more OS-specific, AF_UNIX sockets (at least on Linux) have
a feature called ancillary messages that allow passing file
descriptors between processes. It's currently not supported by the
Python socket lib, but one of these days... . But I don't think
Windows has anything like it.


It can be done on on Windows.

http://tangentsoft.net/wskfaq/articl...g-sockets.html
--
--Bryan
Sep 8 '05 #11

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

Similar topics

0
by: James Lamanna | last post by:
So I've created a thread with the threading.Thread class, but I want to be able to kill this thread because it does a select() with a timeout of None. Is there any way to send a signal only to...
6
by: ll | last post by:
Hi, How to specify the timeout value(ms) for a thread? Thanks.
2
by: Sgt. Sausage | last post by:
New to multi-threading (less than 24 hours at it <grin>) Anyway, it's all making sense, and working fairly well thus far, but I'm having a minor issue I'm not sure how to get around. I've got...
3
by: Raghu Raman | last post by:
Hi, am in c#.net project. am using the session object for some logic. e-g:session="Add"; i can refer this by using if(Session=="Add") { logic
0
by: puff | last post by:
When interfacing to a COM object, is it possible to pump messages in a thread? I'm working on an application that automates IE and needs to monitor IE events (yes I know about Pamie). I'm able...
8
by: Rob | last post by:
Hello, I've got an issue where a process in a third party application has a dll which while exiting sort of freezes and runs away with processor cycles. i've written a block of code so that I...
1
by: Chrace | last post by:
Hi all, I have a problem with with Thread.Join( Timeout ) where the timeout never occurs. I basically need to make a connection to an AS400 box which works fine. Once in a blue moon the AS400...
6
by: Roger Heathcote | last post by:
sjdevnull@yahoo.com wrote: <snip> Fair point, but for sub processes that need to be in close contact with the original app, or very small functions that you'd like 100s or 1000s of it seems...
3
by: yeye.yang | last post by:
hey everybody Does everybody can help me or give me some advise for the cross thread exception catch Here is what I want to do: I have 2 classes "Scenario" and "Step", which have a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.