![]() ![]() Since Python is by default "synchronous" and not event-loop-based language it may be a bit hard to get accustomed to async/await pattern in Python. # if list of updates is filled up to BATCH_SIZE push data to databaseĪwait collection.bulk_write(updates, ordered=False) U = UpdateOne(filter=filter_, update=update_, upsert=True) # this defines batch size that will be added to database at once :param very_big_list: List with a lot of items. # Convert the cursor to a list of items right away.Īsync def bulk_save(very_big_list: list = list()): Iterate over the cursor asynchronouslyĪwait do_something_in_an_async_worker_method(item)Īsynchronously converts cursor to a list with items Processes these by using another asynchronous methodĬollection = db.get_collection("mycollection")Ĭursor = collection.find(filter=filter_, projection=projection_) This method finds items from MongoDB collection and There's a really good async driver API for MongoDB: Motor It gets the job done and even more but where's the fun in all that old school serial, non- async processing? There's an old good Python API for MongoDB - it's called PyMongo. My brain still has 2 halves but these are not called "left half" and "right half" any more but "JS half" and "Python half" ) Half a brain still does the job, haaa:) ![]() And so on and on.įor Python-only developers it may be a bit hard to switch between languages-contexts but it's doable. You can copy-paste code and objects between your IDE/project and MongoDB shell or MongoDB IDE. MongoDB shell has the same language that your code runs in - JavaScript. ![]() If you're a seasoned Node.js developer like myself it feels very comfortable and cozy. MongoDB itself feels very "javascriptish" so to say. Here we are talking about accessing the database, and namely MongoDB. Be it disk reads/writes or accessing the database. Async/await will come extra handy in those cases when there's a lot of I/O operations involved. It has its limits and risks but for simple cases it does the job. With async processing developer can run things in parallel without too much hassle and tinkering with threads or processes. how to use MongoDB asynchronously in Python Async/await - why bother at all?įirst and foremost reason for me is concurrency. However, I try to keep this post short and will focus on a specific task: One big difference is syntax - in JS whitespaces in the code (almost) don't matter while in Python these are important. It's also very similar to JS with its somewhat loose-but-not-so-loose typing, variable assignment etc. ![]() But it's very clear, concise and developers can be really productive in it. Python feels like "old school" language when compared to JavaScript, it really feels old sometimes. But it seems that async processing is a lot less common in Python. By now almost everyone knows or at least has heard about " async/await" paradigm on JavaScript/ Node.JS. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |