Answer the question
In order to leave comments, you need to log in
What is the best way to write to a python file when working asynchronously?
Good afternoon. It is necessary to write the data to a file (text .csv). There is an option to do it through asynchronous aiofiles, but I came across an article in which it is advised to simply write to a file
with open(file, "w") as out: # regular file I/O
async for s in network_request(): # asyncio for slow network work. measure it!
out.write(s) # should be really quick, measure it!
Answer the question
In order to leave comments, you need to log in
Bad advice. You can do without aiofiles, but use loop.run_in_executor()
.
A lot of factors influence.
If you need to write a few kilobytes to a hard disk, then you can do so.
And if then it suddenly turns out that you need to write several tens of megabytes to a remote storage, then problems may arise.
So it depends on the problem at hand.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question