【发布时间】:2022-12-22 01:16:37
【问题描述】:
我有 thread1、thread2 和 thread3、全局变量 x 和三个不同的递增函数 x,
import threading
import time
#check = threading.Condition()
x=1
def add_by1():
global x
x+=1
time.sleep(1)
print(x)
def add_by2():
x+=2
time.sleep(1)
print(x)
def add_by3():
x+=3
time.sleep(1)
print(x)
if __name__==__main__:
threading.Thread(target=add_by1).start()
threading.Thread(target=add_by2).start()
threading.Thread(target=add_by3).start()
# I want the output should print..
"""
2
4
7
8
10
13
14
16
19
and so on ..
"""
我可以使用Condition()吗?如果可以的话怎么办?我可以使用其他线程类吗?如何在这些函数上插入一些代码?
【问题讨论】:
-
你想用
threading.Condition做什么?您是否在多线程环境中阅读过what a Condition does? -
我只是想也许它会解决问题
标签: python multithreading variables global python-multithreading