如何强制线程停止 [英] How to force a thread to stop

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

问题描述

大家好,


有没有办法让创建和启动线程的程序也停止

吧。

(我的用法是暂停)。


例如


thread = threading.Thread(target = Loop.testLoop)

thread.start()#这个线程预计在一秒钟内完成
thread.join(2)#Or time.sleep(2)?


如果thread.isAlive():

#thread可能遇到问题并挂起

#这里应该停止什么线程???? ??


请注意,我不想改变目标(太多),因为存在许多可能的

目标,

汇集了数千行代码。


谢谢,

Hans

Hi all,

Is there a way that the program that created and started a thread also stops
it.
(My usage is a time-out).

E.g.

thread = threading.Thread(target=Loop.testLoop)
thread.start() # This thread is expected to finish within a second
thread.join(2) # Or time.sleep(2) ?

if thread.isAlive():
# thread has probably encountered a problem and hangs
# What should be here to stop thread ??????

Note that I don''t want to change the target (too much), as many possible
targets exist,
together thousands of lines of code.

Thanks,
Hans

推荐答案



Hans写道:

Hans wrote:

大家好,


有没有办法创建并启动一个线程的程序也会停止

它/>
(我的用法是暂停)。


例如


thread = threading.Thread(target = Loop .testLoop)

thread.start()#这个帖子预计会在一秒内结束
thread.join(2)#Or Time.sleep(2)?
Hi all,

Is there a way that the program that created and started a thread also stops
it.
(My usage is a time-out).

E.g.

thread = threading.Thread(target=Loop.testLoop)
thread.start() # This thread is expected to finish within a second
thread.join(2) # Or time.sleep(2) ?



不,Python没有线程方法,而且它的缺席不是

的疏忽。线程通常还有很多重要的事情需要做,例如释放锁定共享数据的
;在任意时间杀死它们

往往会使系统处于不一致状态。

No, Python has no threadicide method, and its absence is not an
oversight. Threads often have important business left to do, such
as releasing locks on shared data; killing them at arbitrary times
tends to leave the system in an inconsistent state.


if thread.isAlive():

#thread可能遇到问题并挂起

#这里应该停止线程??????
if thread.isAlive():
# thread has probably encountered a problem and hangs
# What should be here to stop thread ??????



此时,为时已晚。尝试编写代码,这样你的线程就不会挂起。


当你想要终止程序时,Python会让你安排线程死掉,线程''的Thread.setDaemon()。

-

--Bryan

At this point, it''s too late. Try to code so your threads don''t hang.

Python does let you arrange for threads to die when you want to
terminate the program, with threading''s Thread.setDaemon().
--
--Bryan


嗨!

>> " threadicide"方法
>> "threadicide" method



我喜欢这个词...


Michel Claveau

I like this word...

Michel Claveau


在消息< 11 ********************** @ m73g2000cwd.googlegroups .com> ;,
br ************ ***********@yahoo.com 写道:
In message <11**********************@m73g2000cwd.googlegroups .com>,
br***********************@yahoo.com wrote:

Python没有线程方法,它的缺席不是

疏忽。线程通常还有很多重要的事情需要做,例如释放锁定共享数据的
;在任意时间杀死它们

往往会使系统处于不一致状态。
Python has no threadicide method, and its absence is not an
oversight. Threads often have important business left to do, such
as releasing locks on shared data; killing them at arbitrary times
tends to leave the system in an inconsistent state.



也许避免线程和使用进程的另一个原因是什么?

Perhaps another reason to avoid threads and use processes instead?


这篇关于如何强制线程停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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