线程,线程;如何杀死一个线程? [英] thread, threading; how to kill a thread?

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

问题描述

问候Python人员;


我对线程应用程序的经验有限,而且风格很丰富

分叉重量级多处理应用程序。


在Redhat 7.x机器上使用Python 2.3.3。

想知道主python程序是否有一种简单的方法可以杀死

a运行线程?我在''threading''模块中看到守护进程的方式

线程在主程序完成时的行为。


但是假设我们有一个简单的线程运行一个功能类似于;


def timedLoop():

而True:

time.sleep(10)

doSomething()


我想要做的就是立即从主要的
程序中停止该线程,目前还不清楚如何做到这一点。我看到在运行循环函数的线程对象上使用

del(threadObj)

什么都不做。


没有注意到文档中有任何明显的东西,如''kill''方法或

类似。我不一定要主程序退出,只需要
杀死一个或多个线程。


锁,条件,信号,信号?


ARG!为了天堂,我错过了什么?


谢谢

-

---------- -------------------------------------------------- -------------------

Jerry Sievers 305 854-3001(主页)WWW电子商务顾问

305 321- 1144(手机 http://www.JerrySievers.com/

Greetings Pythonists;

I have limited experience with threaded apps and plenty with old style
forked heavyweight multi-processing apps.

Using Python 2.3.3 on a Redhat 7.x machine.

Wondering if there is a simple way from a main python program to kill
a running thread? I see with the ''threading'' module the way daemonic
threads behave when the main program finishes.

But suppose we have a simple thread running a function like;

def timedLoop():
while True:
time.sleep(10)
doSomething()

All I am trying to do is stop that thread immediatly from the main
program and it is unclear how to do this. I see that using
del(threadObj) on the thread object that''s running the loop function
does nothing.

Didn''t notice anything obvious in the docs like a ''kill'' method or
similar. I don''t necessarily want the main program to exit, just to
kill one or more threads.

Locks, conditions, semafores, signals?

ARG! For heavens sake, what am I missing?

Thanks
--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/

推荐答案

Jerry Sievers写道:
Jerry Sievers wrote:
我对线程应用程序的经验有限,并且有很多旧样式
分叉重量级多处理应用程序。

想知道主python程序是否有一种简单的方法来杀死正在运行的线程?
I have limited experience with threaded apps and plenty with old style
forked heavyweight multi-processing apps.

Wondering if there is a simple way from a main python program to kill
a running thread?



没有办法在Python中''杀死'一个线程。你需要做

类似


def timedLoop():

未完成时:

time.sleep(10)

doSomething()


显然,应该完成实现为

线程对象的实例变量而不是全局,它应该由

setDone()方法设置,它执行任何必要的锁定。是的,线程

不会立即终止,但只有下次循环测试才能执行
.


Aaron


There is no way to ''kill'' a thread in Python. You will need to do
something like

def timedLoop():
while not done:
time.sleep(10)
doSomething()

Clearly, done should be implemented as an instance variable of the
thread object rather than a global, and it should probably be set by a
setDone() method that does any necessary locking. And yes, the thread
will not terminate immediately, but only the next time the loop test is
executed.

Aaron


Jerry Sievers写道:
Jerry Sievers wrote:
想知道主python程序是否有一种简单的方法可以杀死
运行线程?


No.

我在''线程'模块中看到主程序完成时守护程序线程的行为方式。


这是几种方法中的一种,其中任何一种 - 或者没有

- 可能适合你。


是什么驱动了您对此行为的需求?答案

将决定最好的方法。

我想做的就是立即停止主线


定义立即。在回答时,请考虑诸如内核调用中阻塞的线程之类的问题,例如在套接字上接收

数据,以及数据完整性问题(例如,发生的事情

如果通过立即表示在当前字节码的末尾

指令,如果这意味着您可能有系统

数据结构已损坏?)。

没有注意到文档中有任何明显的东西,比如''kill''方法或
类似。我不一定要主程序退出,只是为了杀死一个或多个线程。
Wondering if there is a simple way from a main python program to kill
a running thread?
No.
I see with the ''threading'' module the way daemonic
threads behave when the main program finishes.
This is one of several approaches, any of which -- or none of
which -- might be suitable for you.

What drives your need for this behaviour? The answer
will determine the best approach.
All I am trying to do is stop that thread immediatly from the main
Define "immediately". In answering please consider issues such
as threads that are blocked in kernel calls, such as receiving
data on a socket, as well as data integrity issues (e.g. what happens
if by "immediately" you mean "at the end of the current bytecode
instruction", and if that implies that you may have system
data structures that are corrupted?).
Didn''t notice anything obvious in the docs like a ''kill'' method or
similar. I don''t necessarily want the main program to exit, just to
kill one or more threads.




用Google搜索列表/新闻组档案透过

过去的讨论和可能的一些有用的回应。


-Peter



Searching the list/newsgroup archives with Google will reveal
past discussions and possibly some useful responses.

-Peter


Aaron Bingham写道:
Aaron Bingham wrote:
没有办法在Python中杀死一个线程。你需要做

def timedLoop()之类的事情:
没有完成:
time.sleep(10)
doSomething()
There is no way to ''kill'' a thread in Python. You will need to do
something like

def timedLoop():
while not done:
time.sleep(10)
doSomething()




有关使用threading.Event作为标志的示例,请参阅此配方:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65448


肯特



See this recipe for an example using a threading.Event as the flag:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/65448

Kent


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

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