lrpri ya
lrpri ya

Reputation: 73

call a method Asynchronously from another method

I have a collect_data method which is called from another method create. I want to call this collect_data method asynchronously as the user need not wait for response from function create.

 def create(obj):
     "do something in database"
     "return ID for newly created data in DB"
     collect_data(obj, newly_created_id)
     return newly_created_id

 def collect_data(obj, newly_created_id):
     "collect data from other methods."
     "Put the data in MQ."

So, i want to run this collect_data asynchronously so that user need not wait for the newly_created_id until the data is put in ActiveMQ.

I tried multiprocessing, something like below.

 import multiprocessing as mp
 def create(obj):
     pool = mp.Pool()
     "do something in database"
     "return ID for newly created data in DB"
     pool.apply_async(collect_data, args=(obj, newly_created_id))
     return newly_created_id

But this does not seem to run asynchronously. To test I inserted a sleep for 2 mins in collect_data. Function create did not return the newly_created_id immediately.

Can someone please help.

Upvotes: 0

Views: 68

Answers (1)

quikst3r
quikst3r

Reputation: 1853

from multiprocessing import Pool
import time
import random

class DATA:
    def __init__(self, obj, new_id):
        self.obj = obj
        self.new_id = new_id

def collect_id(data):
    print 'collecting data for obj {0} with id {1}'.format(data.obj, data.new_id)

def create(obj):
    pool = Pool()
    # "do something in database"
    # "return ID for newly created data in DB"
    newly_created_id = int(random.random()*10)
    data = DATA(obj, newly_created_id)
    r  = pool.map_async(collect_id, [data])
    return newly_created_id


new_id = create({'some':'object'})
print "new id: {0}".format(new_id)

You will see it outputs the ID before it outputs that it's collecting the data, showing its happening async in the background

Upvotes: 1

Related Questions