我有一个线程在建设中,我需要在没有任何全局变量的情况下从它那里获取数据。是否可以在没有线程块或其他东西的情况下实时获取数据?请帮助我。
def one():
while True:
a = data1
something gonna here with var b from thread two
def two():
while True:
b = a from thread one
something gonna here
def main():
th1 = Thread(target=one)
th2 = Thread(target=two)
th1.start()
th2.start()
something gonna here with var a and var b
你可以使用multiprocessing.Pipe或者multiprocessing.Queue在python中向不同的线程和进程发送数据。
管道的实现是这样的
from multiprocessing import Pipe
def one(connA):
while True:
a = data1
connA.send([a])
something gonna here with var b from thread two
def two(connB):
while True:
b = connB.recv() #receives [a] from thread one
connB.send([b]) #send [b] to main thread
something gonna here
def main():
A,B=Pipe()
th1 = Thread(target=one,args=(A))
th2 = Thread(target=two,args=(B))
th1.start()
th2.start()
something gonna here with var a and var b
b=A.recv() # receive var b from