Skip to content

add try_recv method for python#1241

Merged
haixuanTao merged 3 commits intomainfrom
add-try-recv-method-for-python
Nov 30, 2025
Merged

add try_recv method for python#1241
haixuanTao merged 3 commits intomainfrom
add-try-recv-method-for-python

Conversation

@haixuanTao
Copy link
Copy Markdown
Collaborator

this function enables to check for input without waiting for it to resolve

@phil-opp
Copy link
Copy Markdown
Collaborator

How does the returned python object look when None is returned? Perhaps we should add that to the docs?

@haixuanTao
Copy link
Copy Markdown
Collaborator Author

I thought that Closed channel would result in the TryRecvError::Closed error.

Can it be None?

@phil-opp
Copy link
Copy Markdown
Collaborator

I thought that Closed channel would result in the TryRecvError::Closed error.

Can it be None?

Ah sorry, I was confused. There is no None, but there is TryRecvError::Empty instead. It looks like this currently also mapped to a Python exception with a string message. Is there a good way to differentiate an empty from a closed channel this way? This would be useful imo because you might want to handle a closed channel differently than an empty one (e.g. break vs wait for a while).

@haixuanTao
Copy link
Copy Markdown
Collaborator Author

I thought that Closed channel would result in the TryRecvError::Closed error.
Can it be None?

Ah sorry, I was confused. There is no None, but there is TryRecvError::Empty instead. It looks like this currently also mapped to a Python exception with a string message. Is there a good way to differentiate an empty from a closed channel this way? This would be useful imo because you might want to handle a closed channel differently than an empty one (e.g. break vs wait for a while).

Yeah, so for now it only going to be text based but in the future we could add custom exception

@phil-opp
Copy link
Copy Markdown
Collaborator

Ok, I guess you could work around this by checking the new is_empty before calling try_recv.

Maybe it would still be better to return an Option instead, which would map to Python's Optional. So basically return None on empty and throw an exception only if finished. This would be more intuitive in my opinion. It would also match the drain method more closely, which also returns an empty value if no events are ready, instead of throwing an exception.

I don't feel strongly about this though, so feel free to merge this as-is if you prefer.

@haixuanTao
Copy link
Copy Markdown
Collaborator Author

Ok, I guess you could work around this by checking the new is_empty before calling try_recv.

Maybe it would still be better to return an Option instead, which would map to Python's Optional. So basically return None on empty and throw an exception only if finished. This would be more intuitive in my opinion. It would also match the drain method more closely, which also returns an empty value if no events are ready, instead of throwing an exception.

I don't feel strongly about this though, so feel free to merge this as-is if you prefer.

Sounds good, to me. I think it's a bit ugly to have

try:
  node.try_recv()

which would make the second try tautological.

@haixuanTao haixuanTao merged commit 7da7887 into main Nov 30, 2025
50 checks passed
@haixuanTao haixuanTao deleted the add-try-recv-method-for-python branch November 30, 2025 05:03
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

Successfully merging this pull request may close these issues.

2 participants