Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Which of the methods are coroutine-safe? #81

Closed
iffybug opened this issue Jun 4, 2023 · 2 comments
Closed

Which of the methods are coroutine-safe? #81

iffybug opened this issue Jun 4, 2023 · 2 comments

Comments

@iffybug
Copy link
Contributor

iffybug commented Jun 4, 2023

Can I share a file/reader/writer object in different coroutines without Lock? Which methods are coroutine-safe?

@mosquito
Copy link
Owner

mosquito commented Jun 5, 2023

TL/DR: Writer - Yes, Reader - Yes, LineReader - Probably yes. However, the point is not quite clear, since these are objects that just contain a pointer to an offset, they are cheaper to copy and send each copy to the diferrent coroutines in my opinion.

I think questions like this has to do with some misunderstanding of exactly how this library designed under the hood. It's probably my bad, fail to explain this in the docs.

There is AIOFile, which is like a low-level abstraction that owns an instance of one of the implementations of AsyncioContext. It is a very simple object that can read and write only by offset, for that you get absolute competitiveness when reading and writing, without having to block anything.

So, objects of class BinaryFileWrapper and TextFileWrapper which is what the async_open function returns and mimicking the normal open in python. They have a caret that points to the file offset, and of course there are locks for read and write that do not allow a race between the operations and the value in the caret. The dark side of this simplicity is the lost read and write concirrency.

However, the library has some helpers that allow you to use one AIOFile concurrently. If you imagine that the user code has different coroutine roles which have different responsibilities, for example one writes data, the others only reads, then you can keep one AIOFile object and pass it wrapped in objects Writer, Reader and LineReader to this coroutines.

This is not the first question in this vein, I do not know how best to reflect this in the documentation, it would be great if someone could help me.

@iffybug
Copy link
Contributor Author

iffybug commented Jun 5, 2023

Thanks for the timely reply! After some investigation, I'm using caio with Lock now, which suits me better. Great work for these two libraries!

@iffybug iffybug closed this as completed Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants