如何在一段时间后停止 运行 我的线程?
How to stop running my threads after a period of time?
我需要在一段时间后停止 运行 我的线程,在这个例子中我只放了 120 秒。我尝试使用这种方法,但它不起作用。
from threading import Thread
from Queue import Queue
import os
import time
timeout = 120 # [seconds]
timeout_start = time.time()
#while True :
def OpenWSN ():
os.system("./toto")
def Wireshark():
os.system(" tshark -i tun0 -T ek -w /home/ptl/PCAP_Brouillon/Sim_Run3rd.pcap > /dev/null ")
def wrapper1(func, queue):
queue.put(func())
def wrapper2(func, queue):
queue.put(func())
q = Queue()
Thread(target=wrapper1, args=(OpenWSN, q)).start()
Thread(target=wrapper2, args=(Wireshark, q)).start()
#print (time.time())
print ("***************** End Simulation *************************")
os.system("quit")
我认为这就是您要实现的目标:
import threading
from queue import Queue
import os
import time
timeout = 120 # [seconds]
timeout_start = time.time()
def OpenWSN ():
print( "OpenWSN:")
os.system("echo -OpenWSN-")
def Wireshark():
print( "Wireshark:")
os.system("echo -Wireshark-")
def wrapper1(func, queue):
queue.put(func())
def wrapper2(func, queue):
queue.put(func())
q = Queue()
threading.Thread(target=wrapper1, args=(OpenWSN, q)).start()
threading.Thread(target=wrapper2, args=(Wireshark, q)).start()
cv = threading.Condition()
cv.acquire()
cv.wait( timeout )
print ("***************** End Simulation *************************")
print (" Simulation Time: {0}s".format( time.time() - timeout_start) )
os.system("echo -exit-")
这会产生以下输出:
C:\temp\StackExchange\StopRunningThread>python -B stop-running-thread.py
OpenWSN:
Wireshark:
-OpenWSN-
-Wireshark-
***************** End Simulation *************************
Simulation Time: 120.04460144042969s
-exit-
那里发生了什么 - 您正在启动两个线程,每个线程在系统中启动单独的进程。在上述线程启动后,您 return 到您的主线程,分配一个 "lock" 并等待直到发出此锁信号或发生超时。
在这种特殊情况下,没有人发出锁定信号,因此完成应用程序的唯一机会是等到超时发生。
我会扩展您的应用程序,它会在每个线程函数中发出锁定信号,因此只有当两个线程函数都终止时,我们才能终止主线程。
但这不是你问题的一部分,所以我假设你可以不打信号就离开。
我需要在一段时间后停止 运行 我的线程,在这个例子中我只放了 120 秒。我尝试使用这种方法,但它不起作用。
from threading import Thread
from Queue import Queue
import os
import time
timeout = 120 # [seconds]
timeout_start = time.time()
#while True :
def OpenWSN ():
os.system("./toto")
def Wireshark():
os.system(" tshark -i tun0 -T ek -w /home/ptl/PCAP_Brouillon/Sim_Run3rd.pcap > /dev/null ")
def wrapper1(func, queue):
queue.put(func())
def wrapper2(func, queue):
queue.put(func())
q = Queue()
Thread(target=wrapper1, args=(OpenWSN, q)).start()
Thread(target=wrapper2, args=(Wireshark, q)).start()
#print (time.time())
print ("***************** End Simulation *************************")
os.system("quit")
我认为这就是您要实现的目标:
import threading
from queue import Queue
import os
import time
timeout = 120 # [seconds]
timeout_start = time.time()
def OpenWSN ():
print( "OpenWSN:")
os.system("echo -OpenWSN-")
def Wireshark():
print( "Wireshark:")
os.system("echo -Wireshark-")
def wrapper1(func, queue):
queue.put(func())
def wrapper2(func, queue):
queue.put(func())
q = Queue()
threading.Thread(target=wrapper1, args=(OpenWSN, q)).start()
threading.Thread(target=wrapper2, args=(Wireshark, q)).start()
cv = threading.Condition()
cv.acquire()
cv.wait( timeout )
print ("***************** End Simulation *************************")
print (" Simulation Time: {0}s".format( time.time() - timeout_start) )
os.system("echo -exit-")
这会产生以下输出:
C:\temp\StackExchange\StopRunningThread>python -B stop-running-thread.py
OpenWSN:
Wireshark:
-OpenWSN-
-Wireshark-
***************** End Simulation *************************
Simulation Time: 120.04460144042969s
-exit-
那里发生了什么 - 您正在启动两个线程,每个线程在系统中启动单独的进程。在上述线程启动后,您 return 到您的主线程,分配一个 "lock" 并等待直到发出此锁信号或发生超时。 在这种特殊情况下,没有人发出锁定信号,因此完成应用程序的唯一机会是等到超时发生。 我会扩展您的应用程序,它会在每个线程函数中发出锁定信号,因此只有当两个线程函数都终止时,我们才能终止主线程。 但这不是你问题的一部分,所以我假设你可以不打信号就离开。