質問

I have to read some data coming across the network, and then insert it into a MongoDB. All of this is done using a Python script. Is there any way to do a nonblocking insert into Mongo from Python so I can continue to process the data coming over the network while the Mongo server handles the insert?

役に立ちましたか?

解決

Yes. There are three possibilities:

  1. Do an unacknowledged insert: collection.insert(documents, w=0). The call to insert() returns as soon as the data has been written to the socket's output buffer, and your code does not block awaiting acknowledgment from MongoDB. You'll get no error reporting to your application. More info about write concerns here and here.
  2. Add batches of documents to a Queue, and in a separate thread pop from the queue and insert into MongoDB: collection.insert(q.get()).
  3. Port your application to Tornado and Motor as James Mills suggested; this seems to require the most learning of the three and is probably overkill.

他のヒント

Give Motor a try. It is an Asynchronous Mongo Drivers for the Tornado Web Framework.

Otherwise there are other options:

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top