【问题标题】:pausing a thread from a function and resuming it暂停函数中的线程并恢复它
【发布时间】:2014-10-23 14:06:24
【问题描述】:

我的程序中有很多线程,如下所示。我想在从线程调用一个特定函数时暂停所有线程,然后在该函数的函数执行完成或延迟 1 秒后恢复。例如在下面的代码中,我想在执行 switch3 时暂停线程 switch1 和 switch2,然后在 switch2 完成执行后或延迟 1 秒后恢复这些线程。

谁能告诉我如何在下面的代码中实现?

def switch1():
    if (buttonpressed ==1):
     print 1

def switch2():
    if (buttonpressed1 ==1):
     switch3()
     print 1

def switch3():
    if (buttonpressed2 ==1):
     print 1

def main():
    switch1thread=threading.thread(target=switch1)
    switch2thread=threading.thread(target=switch2)
    switch1thread.start()
    switch2thread.start()

【问题讨论】:

    标签: python multithreading


    【解决方案1】:

    没有线程本身的配合,你不能暂停和恢复线程。您要暂停或恢复的线程可以通过检查一些标志来暂停和恢复自身。
    例如。您可以在执行 switch3 之前设置一些标志。如果设置了该标志,switch2switch1 将自行暂停。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 2016-03-24
      • 1970-01-01
      相关资源
      最近更新 更多