Paulo Matos
Paulo Matos

Reputation: 1664

Python Multiprocessing Pool not launching jobs

This is Python 2.7. I have tried successfully the documentation code:

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes
    result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously
    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow
    print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"

Then I developed myself a very simple example unsuccessfully:

import multiprocessing

def run_test_n(n):
    print 'inside test %d' % n
    with open('test%d.log' % n, 'w') as f:
        f.write('test %d ok\n' % n)

def runtests(ntests, nprocs):
    pool = multiprocessing.Pool(processes = nprocs)
    print 'runnning %d tests' % ntests
    print 'over %d procs' % nprocs
    tasks = range(1, ntests + 1)
    results = [pool.apply_async(run_test_n, t) for t in tasks]
    for r in results:
        r.wait()

if __name__ == '__main__':
    runtests(5,5)

In this example, no files are created and no 'inside test...' string is printed.

It's as if run_test_n is never called (it actually never is). Why?

Upvotes: 0

Views: 709

Answers (1)

Jun HU
Jun HU

Reputation: 3314

You are wrong in results = [pool.apply_async(run_test_n, t) for t in tasks]; the correct form is results = [pool.apply_async(run_test_n, (t, )) for t in tasks]

Upvotes: 1

Related Questions