简体   繁体   中英

How to get data from a thread in real time?

I have a thread in while construction and I need to get data from it without any global vars. Is it possible to get data in real time without thread block or something? Please help.

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

You could use multiprocessing.Pipe or multiprocessing.Queue to send data to different threads and processes in python.

The Pipe implementation would be something like this

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM