简体   繁体   中英

some python multiprocessing issue for printing num ber

import multiprocessing

def printt(q):    
    if q.empty():      
        pass
    else:
        data = q.get()
        print data 

if __name__ == "__main__":
    q = multiprocessing.JoinableQueue()
    for i in range(5):
        q.put()
    while 1:
        for i in range(3):
            process = multiprocessing.Process(target=printt, arg=(q,))
            process.start()
            process.join()
        q.join()

Code is show above. My questions is how can i control the redundant processes ,cos when i put the number of item which can divided into a int, the code above is going to raise error.How can i finish my task of print number at the mean time not raising error.

the code to fix this problem:

import multiprocessing

def printt(q):
    while 1:
        if q.empty():
            break
        else:
            data = q.get()
            q.task_done()
            print data


if __name__ == "__main__":
    q = multiprocessing.JoinableQueue()
    for i in range(5):
        q.put(i)
    for i in range(3):
        process = multiprocessing.Process(target=printt, args=(q,))
        process.start()
    q.join()
    print "over"

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