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

pynng sock.recv() not interruptable #106

Open
Sec42 opened this issue May 26, 2022 · 4 comments
Open

pynng sock.recv() not interruptable #106

Sec42 opened this issue May 26, 2022 · 4 comments

Comments

@Sec42
Copy link
Contributor

Sec42 commented May 26, 2022

If I call .recv() on a socket created without recv_timeout, the call isn't interruptable with ^C.

Example:

with pynng.Sub0() as sock:
    sock.subscribe("")
    sock.dial(addr)
    msg = sock.recv()

Hangs indefinitely if I press ^C until a messages comes in.

@ntakouris
Copy link

You can work around this by adding a small timeout, inside a while loop, or while checking if some flag for shutdown is up (ex. to stop a thread)

with pynng.Sub0(...) as sock:
    sock.subscribe("")
    sock.dial(addr)
    while True: # or not stop_request
        try:
            msg = sock.recv(timeout=1.0)
        except pynng.exceptions.Timeout:
            pass 

@codypiersall
Copy link
Owner

This behavior cannot be fixed without changes to the underlying nng library; see an issue I opened a couple years ago for a discussion.

I'll leave this issue open just as a reference.

@aqc-carlodri
Copy link

I have tried your solution @ntakouris and it works, thank you. However, I had to abandon it because try-except statements in Python are very cheap (performance-wise) if there is no exception, but become expensive when the exception occurs. Since I had several IPC channels working together, I was adding a big performance burden on my code.

@aqc-carlodri
Copy link

I was playing around with pyzmq and stumbled into the same issue, and I found this SO reply that actually worked. Just sharing for now, haven't tried it with pynng. Will post back if it works.

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