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

Is there a way to have select do one and only one action ? #1

Open
edouardklein opened this issue Jul 24, 2017 · 8 comments
Open

Is there a way to have select do one and only one action ? #1

edouardklein opened this issue Jul 24, 2017 · 8 comments

Comments

@edouardklein
Copy link

I've been banging my head on this problem for some time, and someone showed this library (which looks quite nice, congrats:) to me, but is is unclear to me if the semantics of select are the same as in Go:

Sometimes, in Go, my code relies on the fact that only one of the channel read or write passed to select actually happened.

If I send one value in channel c1 and another value in channel c2, and then select them both, and e.g. c1's read is chosen by the select and I discard its third return value, is there a guarantee that's c2's value is still available, or is there a possibility that it is lost ?

My problem here is that c2 may have been given to another goroutine, which does not have any access to the context where the third return value of the select call exists, and I may need this coroutine to have access to c2's value.

I hope my question is clear, if not, I'll try my best to clarify.

@pothos
Copy link
Owner

pothos commented Jul 24, 2017

Hi,
after (id_, r), cases = await select(cases) it's possible that others have completed and their values are saved into the cases.completed list – if you don't do a select(cases) again or retrieve them manually from the cases.completed list they are gone.

@pothos
Copy link
Owner

pothos commented Jul 24, 2017

It's based on https://docs.python.org/3/library/asyncio-task.html#asyncio.wait and FIRST_COMPLETED – but if you can find out whether .completed is always empty then it could even be removed ;)

@pothos
Copy link
Owner

pothos commented Jul 24, 2017

Nope, it's really needed and can contain values. So you have to process them (maybe forward/put back in your case?).

@edouardklein
Copy link
Author

Thank you very much for your answers :)

Putting them back is a good idea, but I'm not familiar with asyncio enough to know whether this can be done with a guarantee that no other goroutine is putting something before the value we just read.

Maybe putting the asyncio.wait and the putting back of extraneous values in a critical section ?

I'll work on it. Would you consider a pull request in that sense, maybe with a keyword argument to select so that existing code gets the expected behavior, but go-style select is possible ?

If so, I'll propose the matter to one of my interns.

Cheers,

@pothos
Copy link
Owner

pothos commented Jul 24, 2017

This was just an educational project for me, I'm happy if it has a use and promotes async Python ;) and there is much room for improvements here, please provide PR's or maintain a fork of this project. The default event loop is slow and it would be interesting to run it on another one - but even then Python has its limits ;)
Best regards

@navytux
Copy link

navytux commented Dec 3, 2019

For the reference: in my experience getting select semantics right is the key when implementing go-like channels. Some time ago I had to port my code from Go to Python and for that implemented pygolang which provides working select including working synchronous select - select sends.

@dumblob
Copy link

dumblob commented May 28, 2021

Thanks @navytux - I was looking for a working select and pygolang seems to cut it!

@navytux
Copy link

navytux commented May 31, 2021

You are welcome, @dumblob; thanks for feedback.

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

4 participants