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

问题描述

问候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(手机

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/

推荐答案



没有办法在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





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

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


-Peter



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

-Peter





有关使用threading.Event作为标志的示例,请参阅此配方:



肯特



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

Kent


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

08-04 05:12