import multiprocessing as mp
import time
from time import sleep


def foo(q, id):
    while q.empty() == False:
        print ("%s %s" % (id, q.get()))

        for i in range(1, 5):
            sleep(0.1)

        sleep(0.01)

if __name__ == '__main__':
    mp.set_start_method('spawn')
    q = mp.Queue()
    stime = time.time()
    q.put('001')
    q.put('002')
    q.put('003')
    q.put('004')
    q.put('005')
    q.put('006')
    q.put('007')
    q.put('008')
    q.put('009')
    q.put('010')

    MAX_PROCESS = 20
    p = None
    for i in range (0, MAX_PROCESS):
        p = mp.Process(target=foo, args=(q, i))
        p.start()

    for i in range(0, MAX_PROCESS):
        p.join()

    etime  = time.time()
    print("%s seconds" % (etime - stime))

+ Recent posts