Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python MultiProcessing.Pool: How to use with no iterable?

multi-threaded processes

    p = ThreadPool(processes=10)  # creates a pool of 10 workers
    p.map(function_to_call, iterable)  # calls FUNCTION_TO_CALL with the first item from iterable as parameter
    p.close()  # closes the multi-threaded processes one all threads done

I've been trying to use this model, but what if I want to thread a function which has no parameters. Like run() . What would I put for the 'iterable' space, I've been looking all around and can't find a solution.

like image 436
chris horton Avatar asked Oct 16 '25 06:10

chris horton


1 Answers

The Pool.apply function is what you are looking for. Use Pool.apply_async if you want a non blocking call.

p = ThreadPool(processes=10)
p.apply(function_to_call)
p.close()
like image 184
noxdafox Avatar answered Oct 18 '25 23:10

noxdafox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!