【发布时间】:2017-09-13 12:46:50
【问题描述】:
我从一个 API 获得两个数据流,所以有 3 个线程,主线程,stream1 和 stream2。 Stream1 和 Stream2 需要处理这些数据,完成后将它们存储在 main_value1 和 main_value2 中。
我需要在任何给定时间从主线程读取最后一个值(所以如果我需要这个值并且它仍在处理中,那么我会得到最后一个处理/存储的值),最佳方式是什么?从这里的代码示例中,我需要帮助编写函数 get_main_value1(),当然还有 get_main_value2()
def stream1():
while True:
main_value1 = process()
def stream2():
while True:
main_value2 = process2()
def get_main_value1(): ?
def get main_value2(): ?
def main():
threading.Thread(function=stream1,).start()
threading.Thread(function=stream2).start()
while True:
time.sleep(random.randint(0,10))
A = get_main_value1()
B = get_main_value2()
【问题讨论】:
标签: python multithreading