0

I like to use the iPython to run queries to mongodb. I have lots of queries written as different function calls. Mongo can support multiple queries at once but I can't figure out how to use the threading or Multiprocessing module to get a returned value. For instance if I use multiprocessing, I will just use an instance of the Processing class to submit queries then have the interpreter return control to me so I can do other things.

#queries.py
import pymongo
cursor = pymongo.MongoClient()['my_database']['my_collection']
def query_something(length):
    return cursor.find({"field":{"$gt":length}})

#its easy to do
ipython
>import queries
>thirty = queries.query_something(30)
####but I have to wait for this query to execute before I get the interpreter back
>thrity_five = queries.query_something(35)
###wait again

#now I want to thread this so I can do multiple instances
ipython
>import queries
>from multiprocessing import Process
>p1 = Process(target=queries.query_something,args=30)
>p2 = Process(target=queries.query_something,args=35)
>p1.start()
>p2.start()
#how do I get the return values?

Now when I look at the operations, both of these queries are running. But how do I return the values from the query_something function. After the query is complete I have tried p1.join() but nothing is there? How can I start both of these processes without having to wait for the ipython interpreter to return?

J

jwillis0720
  • 4,329
  • 8
  • 41
  • 74
  • See answer at http://stackoverflow.com/questions/2846653/python-multithreading-for-dummies – flup Sep 30 '13 at 06:22

1 Answers1

0

Check out following documentation: Exchanging objects between processes

shantanoo
  • 3,617
  • 1
  • 24
  • 37