超时后杀死线程 [英] killing thread after timeout

查看:103
本文介绍了超时后杀死线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。


我要编写python脚本,它将从

socket读取python命令,运行它并将一些值返回socket。


我的问题是,我需要一些超时。我需要说一下:


os.system(" someapplication.exe")


如果等待的时间更长,就把它杀掉比让我们说100秒


我想在单独的线程上调用命令,然后在给定超时后 -

杀死线程,但我意识到(之后)阅读Usenet档案)那里有

无法用Python杀死一个线程。


我怎样才能实现我的脚本呢?


PS。它应该是可移植的 - Linux,Windows,QNX等

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

推荐答案

阅读更多存档后我认为解决方案可能是提出一个

超时后异常,但如何移植呢?
After reading more archive I think that solution may be to raise an
Exception after timeout, but how to do it portable?


你需要一个处理其他线程的监视程序线程。

监视程序线程只是构建了一个启动线程的表格,并且在一定的秒数后退出
,使这些线程到期并杀死它们。

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.


Jacek Pop3awski写道:
Jacek Pop3awski wrote:
你好。

我要编写python脚本,它将从 socket,运行它并将一些值返回socket。

我的问题是,我需要一些超时。我需要举例说:

os.system(" someapplication.exe")

并将其杀死,如果它等待的时间超过让我们说100秒

我想在单独的线程上调用命令,然后在给定超时后 -
杀死线程,但我意识到(在阅读Usenet存档后)有没有办法杀死一个Python中的线程。

然后如何实现我的脚本?

PS。它应该是可移植的 - Linux,Windows,QNX等
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



可能最简单的方法是使用select超时(参见

docs for library module选择)。例如。


a,bc = select.select([mySocket],[],[],timeout)

如果len(a)== 0:

打印''我们超时''

其他:

打印''插座有东西给我们''

史蒂夫


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


这篇关于超时后杀死线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆