Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 暂停函数中的线程并将其恢复_Python_Multithreading - Fatal编程技术网

Python 暂停函数中的线程并将其恢复

Python 暂停函数中的线程并将其恢复,python,multithreading,Python,Multithreading,我的程序中有许多线程,如下所述。我想在从线程调用某个特定函数时暂停所有线程,然后在该函数的函数执行完成后或延迟1秒后恢复。例如,在下面的代码中,我想在执行switch3时暂停switch1和switch2线程,然后在switch2完成执行后或在1秒后恢复这些线程 有人能告诉我如何在下面的代码中实现吗 def switch1(): if (buttonpressed ==1): print 1 def switch2(): if (buttonpressed1 ==1)

我的程序中有许多线程,如下所述。我想在从线程调用某个特定函数时暂停所有线程,然后在该函数的函数执行完成后或延迟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()

如果没有线程本身的配合,就不能暂停和恢复线程。要暂停或恢复的线程可以通过检查一些标志来暂停和恢复自身。
例如,您可以在执行
开关3
之前设置一些标志<如果设置了该标志,则代码>开关2和开关1将暂停