halcyonjuly7
halcyonjuly7

Reputation: 315

Gevent Pool apply_async

How do you prevent your program for stopping with pool.apply_async in gevent what I mean is

import gevent
from gevent.monkey import patch_all
from gevent.pool import Pool

pool = Pool(10)

def print_num(num):
    print(num)


numbers  = [1,2,3,4,5,6,7,8,9,10]

for number in numbers:
   self.pool.apply_async(print_num, args=(number,))

now if we ran the code above as is the program will close and the workers havent finished everything

if we add a join the program still exits.

how do we wait for all the workers to finish what they need to do in

pool.apply_async in gevent

Upvotes: 0

Views: 1573

Answers (1)

Luv
Luv

Reputation: 496

This works just fine for me.

import gevent
from gevent.monkey import patch_all
from gevent.pool import Pool

pool = Pool(10)

def print_num(num):
    print(num)


numbers  = [1,2,3,4,5,6,7,8,9,10]

for number in numbers:
   pool.apply_async(print_num, args=(number,))

pool.join()

Concurrency is not multi-threading

When the pool.join is not called then the greenlets are never scheduled. When the join is called then the greenlets do not run as a separate thread. Concurrency versus parallelism.

Upvotes: 1

Related Questions