Python: Update Local Variable in a Parallel Process from Parent Program

后端 未结 3 2090
攒了一身酷
攒了一身酷 2021-01-26 07:14

I am relatively new to programming, and what I am asking might be a task that is not possible. What I want to do is start a parallel process, which will continuously run until

3条回答
  •  遥遥无期
    2021-01-26 08:05

    Use a queue. (python 2 is Queue)

    There are a few ways to consume from the queue. The naive implementation (i.e. the one that has a race condition) is simply to do if q.empty(). This is bad practice but wouldn't hurt you since it's not mission critical if you miss a tick.

    The better method is to use exceptions for flow control:

    q = queue.Queue()
    try:
        q.get(False) #non-blocking get()
        #update value of i, etc
    except queue.Empty:
        #do stuff here as before
    

提交回复
热议问题