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

SELECT casing is not working #50

Closed
runflowcode opened this issue Oct 30, 2017 · 3 comments
Closed

SELECT casing is not working #50

runflowcode opened this issue Oct 30, 2017 · 3 comments

Comments

@runflowcode
Copy link

def casing_test2():
    import time
    from datetime import datetime

    def server1(ch):
        time.sleep(6)
        ch.send('from server 1')

    def server2(ch):
        time.sleep(2)
        ch.send('from server 2')

    def main():
        output1 = goless.chan()
        output2 = goless.chan()

        cases = [goless.rcase(output1), goless.rcase(output2)]

        goless.go(server1, output1)
        goless.go(server2, output2)

        # will iterate through all cases, and execute the pick the first available case
        chosen, value = goless.select(cases)

        print(chosen)
        print(value)

    main()

casing_test2()

Outputs:
<goless.selecting.rcase object at 0x7fcbacccf390>
from server 1


The returned result should actually be.
from server 2

Can you explain why this is not working as it should ???
server2 would exit first because it is only sleep for 2 seconds, which is faster than server1 6 seconds.

@rgalanakis
Copy link
Owner

time.sleep is not set up to be 'cooperative'. It just blocks the current thread, so server1 function just executes first, waits 6 seconds, then sends. It doesn't compete with server2 because the server2 sleep call isn't made until server1 is done.

It was a mistake to have this in the examples, my bad.

I added a goless.sleep shim for this: #51

@rgalanakis
Copy link
Owner

Ok, it turns out there is a good reason I never did this. Sleeping in Stackless is not really supported like it is in gevent.

  1. If you try to block on the last tasklet, it errors. I can get around this by using time.sleep if it is the final tasklet.

  2. The custom code required for Stackless sleeping requires a long-running tasklet, or a lot of very tricky state management code for cleanup/teardown of the manager. It's possible, but a total pain.

My guess is that most people know exactly what backend they are running under, and use goless for the semantics. So, you should use gevent.sleep or use gevents monkey patching over time.sleep. If that's a problem, please tell me.

@runflowcode
Copy link
Author

Thanks for the help.

It was helpful to hear about the way how goless work with the correct sleep timer.

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