Tinche / aiofiles

File support for asyncio
Apache License 2.0
2.67k stars 149 forks source link

Many writes to a file #39

Closed sander76 closed 6 years ago

sander76 commented 6 years ago

I want to write to a file many times during execution of my code.

Looking at the examples and the code a thread is created using run_in_executor during the open method of aiofiles. So each time I want to write to the file a new thread is created. Which is expensive I assume ? (am I now developing for a raspberrypi)

Is it possible to dedicate a specific thread to a specific file ? Or should I keep the file open all the time. Do my writes and finally close it ?

Thanks !

Tinche commented 6 years ago

Threads aren't particularly expensive, but ThreadPoolExecutors will reuse threads anyway. So at a certain point, the executor won't spawn new threads but use idle ones from its pool. You can configure the pool size, look into configuring asyncio's thread pool.

sander76 commented 6 years ago

Thanks ! I'll go ahead and try.