Python 3.x 在线程python3中使用time.sleep()
我试图在python3中创建一个简单的Python 3.x 在线程python3中使用time.sleep(),python-3.x,multithreading,sleep,Python 3.x,Multithreading,Sleep,我试图在python3中创建一个简单的线程,其中test1将运行到一定数量,然后sleep,而test2仍将运行,并且当它达到一定数量时将进入睡眠状态。 我的代码是这样的: def test2(count): if count == 8: print("sleep for 4 sec") time.sleep(3.0) print("test2 thread = {}".format(count)) def test1(count): i
线程
,其中test1
将运行到一定数量,然后sleep
,而test2
仍将运行,并且当它达到一定数量时将进入睡眠状态。
我的代码是这样的:
def test2(count):
if count == 8:
print("sleep for 4 sec")
time.sleep(3.0)
print("test2 thread = {}".format(count))
def test1(count):
if count == 5:
print("sleep for 5 sec")
time.sleep(3.0)
print("test1 thread = {}".format(count))
for num in range(0,10):
t1 = threading.Thread(target=test1, args=(num,))
t2 = threading.Thread(target=test2, args=(num,))
t1.start()
t2.start()
import threading
def do_stuff():
print("Stuff on thread {}".format(threading.get_ident()))
print("Main thread {}".format(threading.get_ident()))
t = threading.Thread(target=do_stuff) # Specify what should be running in new thread
t.start() # Dispatch thread
t.join() # Wait until the thread is done
此外,我以前一直在编写python代码,但没有使用线程,现在我想尝试一下,并希望这会很好地结束:)
哦,另外,如果它们重叠,输出也不重要。线程线程()创建新线程,而t1.start()
只需分派它即可
此代码:
for num in range(0,10):
t1 = threading.Thread(target=test1, args=(num,))
t2 = threading.Thread(target=test2, args=(num,))
t1.start()
t2.start()
实际上,每个迭代创建并启动2个新线程。最后你有20个线程+主线程
此外,当您启动线程时,您应该等待它结束,或者作为守护进程线程运行它。在守护线程中,你是说我不在乎你做什么和什么时候结束
基本线程使用情况如下所示:
def test2(count):
if count == 8:
print("sleep for 4 sec")
time.sleep(3.0)
print("test2 thread = {}".format(count))
def test1(count):
if count == 5:
print("sleep for 5 sec")
time.sleep(3.0)
print("test1 thread = {}".format(count))
for num in range(0,10):
t1 = threading.Thread(target=test1, args=(num,))
t2 = threading.Thread(target=test2, args=(num,))
t1.start()
t2.start()
import threading
def do_stuff():
print("Stuff on thread {}".format(threading.get_ident()))
print("Main thread {}".format(threading.get_ident()))
t = threading.Thread(target=do_stuff) # Specify what should be running in new thread
t.start() # Dispatch thread
t.join() # Wait until the thread is done
注意:threading.get_ident()
提供调用此函数的线程的唯一标识符
现在,根据您的示例,如果您想要启动2个独立线程,可以执行以下操作:
import threading
import time
def test2():
for count in range(0, 10):
if count == 8:
print("test2: sleep for 4 sec")
time.sleep(3.0)
print("test2: thread = {}".format(count))
def test1():
for count in range(0, 10):
if count == 5:
print("test 1: sleep for 5 sec")
time.sleep(3.0)
print("test1: thread = {}".format(count))
t1 = threading.Thread(target=test1)
t2 = threading.Thread(target=test2)
t1.start()
t2.start()
t1.join()
t2.join()
但您可能希望同步这些线程,并在“同时”向它们发送一些项目
为了向其他线程发送值,我们可以使用queue.queue。您可以安全地将该值放入一个线程中,第二个线程可以读取该值,或者等待出现某个值(或者多个线程可以写入,多个线程可以读取)
哦,等等。。。我们如何知道线程已经完成了它们的工作,并且我们可以发送另一个值?我们可以再次使用队列。创建新对并在测试?
功能结束时发送,例如True
,然后从这些队列在主循环中等待读取。但是对于发送状态信息,我们应该使用threading.Event
import threading
import time
import queue
def test2(q, e):
while True:
count = q.get() # Get data from the q2 queue
if count == 8:
print("test2: sleep for 4 sec")
time.sleep(3.0)
print("test2: thread = {}".format(count))
e.set() # Inform master the processing of given value is done
def test1(q, e):
while True:
count = q.get() # Get data from the q1 queue
if count == 5:
print("test 1: sleep for 5 sec")
time.sleep(3.0)
print("test1: thread = {}".format(count))
e.set() # Inform master the processing of given value is done
# Creates queues
q1 = queue.Queue()
q2 = queue.Queue()
# Create events
e1 = threading.Event()
e2 = threading.Event()
# Create threads
t1 = threading.Thread(target=test1, args=(q1, e1))
t2 = threading.Thread(target=test2, args=(q2, e2))
# Run threads
t1.start()
t2.start()
# Go through some list or whatever
for num in range(0, 10):
# send num to t1
q1.put(num)
# send num to t2
q2.put(num)
# wait for t1
e1.wait()
# wait for t2
e2.wait()
# Wait until threads are finished with their jobs
t1.join()
t2.join()
现在我们就快到了,但剧本永远不会结束。这是因为test?
函数(线程)在无限循环中等待数据(来自队列q1/q2)。我们需要一些方法告诉他们“好的,就这些人”。因此,我们可以说队列中的无值意味着结束。结果如下:
import threading
import time
import queue
def test2(q, e):
while True:
count = q.get() # Get data from the q2 queue
if count is None: # Exit on None value
return
if count == 8:
print("test2: sleep for 4 sec")
time.sleep(3.0)
print("test2: thread = {}".format(count))
e.set() # Inform master the processing of given value is done
def test1(q, e):
while True:
count = q.get() # Get data from the q1 queue
if count is None: # Exit on None value
return
if count == 5:
print("test 1: sleep for 5 sec")
time.sleep(3.0)
print("test1: thread = {}".format(count))
e.set() # Inform master the processing of given value is done
# Creates queues
q1 = queue.Queue()
q2 = queue.Queue()
# Create events
e1 = threading.Event()
e2 = threading.Event()
# Create threads
t1 = threading.Thread(target=test1, args=(q1, e1))
t2 = threading.Thread(target=test2, args=(q2, e2))
# Run threads
t1.start()
t2.start()
# Go through some list or whatever
for num in range(0, 10):
# send num to t1
q1.put(num)
# send num to t2
q2.put(num)
# wait for t1
e1.wait()
# wait for t2
e2.wait()
# Inform threads to exit
q1.put(None)
q2.put(None)
# Wait until threads are finished with their jobs
t1.join()
t2.join()
注意:您可以使用全局变量,而不是在线程“main”函数中使用参数,因为全局变量或类属性在所有线程中共享。但通常这是一种坏习惯
注意线程带来的问题,例如异常处理不是那么容易。假设函数test1
在调用e.set()
之前引发异常。然后主线程永远不会停止等待e1.wait()
另外,CPython(Python最常见的实现)有一个名为的东西,它基本上(除了一些例外)允许一次只运行一个线程,而其他线程正在休眠
thanx对于如此出色和详细的回答,我会在家里检查,并在我测试完所有内容后接受答案:)