I'm using the threading
module in Python and have read the tutorial here . My intended program flow is that I have 2 functions foo()
and bar()
and the following happens
1) foo()
runs and collects some data stored in data
then stops.
2) bar()
then runs continuously using data
.
3) After 2 minutes foo()
runs again and updates data
while bar()
continues to run using the old version of data
.
4) After the successful update bar()
starts using the new version of data.
5) Loop back to (3)
Now I want to run these as separate threads and my issue lies in how does foo()
tell bar()
it has finished running and the new version of data
is available?
I've read about Queue
but couldn't see exactly how I would use it in this instance.
It depends on how bar consumes the data. If bar has some natural point where it can choose to use existing or new data, it can read a queue filled by foo.
def foo(bar_q):
while True:
data = get_some_data()
# copy or deepcopy if foo will be updating data instead of fetching new
bar_q.put(copy.copy(data))
time.sleep(120)
def bar(bar_q):
# wait for the first data
data = bar_q.get()
bar_q.task_done()
while True:
process_some_data(data)
try:
# get new data if available
data = bar_q.get_nowait()
bar_q.task_done()
except Queue.Empty:
pass
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.