Question

with multiprocessing python library I can launch multiprocess, like

import multiprocessing as mu

def worker(n)
        print "worker:", n
        n = int(1e4)
        for i in range(n):
                for j in range(n):
                        i*j 
        return

if __name__ == '__main__':
        jobs = []
        for i in range(5):
                p = mu.Process(target=worker, args=(i,))
                jobs.append(p)
                p.start()

and I can get the numbers of processors (cpu cores) with

np = mu.cpu_count()

but if I have a list of process, how I can launch without overcharge the processor ?
if I have a quad core, how I can launch first 4 process? and when finish a process launch other.

References

Était-ce utile?

La solution 2

I make this solution

import multiprocessing as mu

def worker(n):
        print "worker:", n
        n = int(1e4/2)
        for i in range(n):
                for j in range(n):
                        i*j
        return

if __name__ == '__main__':
        jobs = []
        for i in range(5):
                p = mu.Process(target=worker, args=(i,))
                jobs.append(p)

        running = []
        np = mu.cpu_count()

        for i in range(np):
                p = jobs.pop()
                running.append(p)
                p.start()

        while jobs != []:
                for r in running:
                        if r.exitcode == 0:
                                running.remove(r)
                                p = jobs.pop()
                                p.start()
                                running.append(p)

Autres conseils

I would suggest side stepping the problem and using multiprocessing.Pool (example, api).

(modified from the example in the docs)

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    num_proc = multiprocessing.cpu_count()
    pool = Pool(processes=num_proc)
    res = pool.map(f, range(10))

Alternately, you can set up a producer/consumer scheme and have a fixed number of long running sub-processes.

A third really quick and dirty way is using one mu.Queue. Note that get blocks until it gets a result back.

import multiprocessing as mu
import time
res = mu.Queue()

def worker(n):
    print "worker:", n
    time.sleep(1)
    res.put(n)
    return

if __name__ == '__main__':
    jobs = []
    np = mu.cpu_count()
    print np
    # start first round
    for j in range(np):
        p = mu.Process(target=worker, args=(j,))
        jobs.append(p)
        p.start()
    # every time one finishes, start the next one
    for i in range(np,15):
        r = res.get()
        print 'res', r
        p = mu.Process(target=worker, args=(i,))
        jobs.append(p)
        p.start()
    # get the remaining processes 
    for j in range(np):
        r = res.get()
        print 'res', r
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top