“运行”的方法我有一个后台线程在我的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. 在ExitDialog上暂停线程
- 5. 暂停工作流
- 6. 在Python中暂停进程或线程
- 7. 多线程暂停第二个线程
- 8. 杀死暂停的线程
- 9. 什么是暂停线程?
- 10. 暂停Windows服务线程?
- 11. Python暂停线程执行
- 12. 暂停/恢复线程?
- 13. “暂停”,线程与属性
- 14. Android:暂停线程几秒
- 15. WPF - 暂停UI线程?
- 16. 暂停Web请求(线程)
- 17. java工作线程中的无限循环违反ScheduledExecutorService暂停
- 18. “暂停”.NET 4中的工作流程?
- 19. 如何暂停工作流程并恢复工作流程?
- 20. 暂停按钮在切换播放/暂停时不工作
- 21. DIR/p暂停不工作...?
- 22. Scrapyd暂停/继续工作
- 23. Android:线程停止工作
- 24. PHP线程停止工作
- 25. 线程停止工作
- 26. 停止线程不工作
- 27. 如何在Pycharm中暂停程序执行(暂停按钮不工作)?
- 28. 在调用方法后暂停线程
- 29. 在线程切换时暂停gdb
- 30. IntelliJ - 在调试时暂停线程
为什么?如果您从UI函数调用方法,它们将在UI线程的上下文中运行,但仍可以访问工作线程内的变量和内容。您可能不得不用这些变量保护这些变量。但是信号量。 – 2012-07-24 12:38:42
对不起,我对Python很新,所以我不确定你的意思。我想从GUI方法中调用工作线程方法(它已经在运行它的运行方法的while循环中)。 – Milad 2012-07-24 12:41:35