python queue使用

2022-05-13 16:56:43 浏览数 (2)

代码语言:javascript复制
"""
 Process class can also be subclassed just like threading.Thread;
 Queue works like queue.Queue but for cross-process, not cross-thread
 """
import os, time, queue
 from multiprocessing import Process, Queue           # process-safe shared queue
 # queue is a pipe   locks/semas
 class Counter(Process):
 label = '  @'
 def init(self, start, queue):                # retain state for use in run
 self.state = start
 self.post  = queue
 Process.init(self)
代码语言:javascript复制
def run(self):                                   # run in newprocess on start()
    for i in range(3):
        time.sleep(1)
        self.state  = 1
        print(self.label ,self.pid, self.state)  # self.pid is this child's pid
        self.post.put([self.pid, self.state])    # stdout file is shared by all
    print(self.label, self.pid, '-')
代码语言:javascript复制
if name == 'main':
 print('start', os.getpid())
 expected = 9
代码语言:javascript复制
post = Queue()
p = Counter(0, post)                        # start 3 processes sharing queue
q = Counter(100, post)                      # children are producers
r = Counter(1000, post)
p.start(); q.start(); r.start()

while expected:                             # parent consumes data on queue                         
    time.sleep(0.5)                         # this is essentially like a GUI,
    try:                                    # though GUIs often use threads
        data = post.get(block=False)
    except queue.Empty:
        print('no data...')
    else:
        print('posted:', data)
        expected -= 1

p.join(); q.join(); r.join()                # must get before join putter
print('finish', os.getpid(), r.exitcode)    # exitcode is child exit status

0 人点赞