A
A
AlexBoss2021-12-06 18:43:24
Python
AlexBoss, 2021-12-06 18:43:24

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!

This approach is really better at the moment, is everything up to date?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2021-12-06
@AlexBoss

Bad advice. You can do without aiofiles, but use loop.run_in_executor().

V
Vindicar, 2021-12-06
@Vindicar

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 question

Ask a Question

731 491 924 answers to any question