“运行”的方法我有一个后台线程在我的wxPython应用程序,以保证GUI作出响应。 有一段时间(true)循环在我的后台线程的“运行”的方法,但我也有其他的方法,我有时从GUI线程中调用。无论如何,当我进入后台线程的另一种方法时停止运行方法?暂停在工作线程
Q
暂停在工作线程
0
A
回答
0
做到像
while(alive):
while(not stopped):
"""
thread body
"""
和别的地方,你将能够暂停线程与
stopped=True
,比使用
stopped = True
alive = False
退出线程
1
让说你有一些c颂这样的:
import threading
import time
class MyWorkerThread(threading.Thread):
def run():
while True:
# Do some important stuff here
foo()
time.sleep(0.5)
def foo():
# Do something important here too
pass
class SomeRandomButton:
def __init__(worker_thread):
self.worker_thread = worker_thread
# Function called when button is clicked
def on_button_clicked():
self.worker_thread.foo();
my_worker_thread = MyWorkerThread()
my_button = SomeRandomButton(my_worker_thread)
# Start thread
my_worker_thread.run()
# Initialize the GUI system (creating controls etc.)
# Start GUI system
GUISystem.run()
上面的代码实际上并没有做任何事情,甚至不会跑,但我会用它来表明,在一个线程对象的函数(MyWorkerThread.foo
)不有到从该特定线程中调用,它可以从任意线程调用。
您可能想了解更多关于多线程的,也许有关semaphores保护由多个线程同时被访问的数据。
相关问题
- 1. 工作线程暂停/恢复实施
- 2. 线程暂停
- 3. 协程暂停工作
- 4. 控制线程暂停和恢复一组工作线程
- 5. 如何在主线程停止/暂停线程/ Activity在android中暂停/停止?
- 6. pthread - 暂停/暂停所有线程
- 7. 暂停和取消暂停线程
- 8. 主线程暂停
- 9. Ruby暂停线程
- 10. 在ExitDialog上暂停线程
- 11. 暂停和停止线程
- 12. 让PyDev暂停在MainThread以外的线程上工作
- 13. 暂停工作流
- 14. 播放/暂停按钮在线工作但不在线
- 15. 在Python中暂停进程或线程
- 16. 多线程暂停第二个线程
- 17. 暂停下载线程
- 18. WPF - 暂停UI线程?
- 19. “暂停”,线程与属性
- 20. 暂停/恢复线程?
- 21. 暂停后台线程()
- 22. 什么是暂停线程?
- 23. 是主线程暂停吗?
- 24. Java InterruptedException暂停线程?
- 25. 暂停Windows服务线程?
- 26. Android:暂停线程几秒
- 27. C# - 多线程暂停
- 28. 暂停线程周期(android)
- 29. 杀死暂停的线程
- 30. 暂停Web请求(线程)
为什么?如果您从UI函数调用方法,它们将在UI线程的上下文中运行,但仍可以访问工作线程内的变量和内容。您可能不得不用这些变量保护这些变量。但是信号量。 – 2012-07-24 12:38:42
对不起,我对Python很新,所以我不确定你的意思。我想从GUI方法中调用工作线程方法(它已经在运行它的运行方法的while循环中)。 – Milad 2012-07-24 12:41:35