Closed koxu1996 closed 7 years ago
Use autobahn.twisted.util.sleep
instead of the Python standard library time.sleep
- the latter is blocking the reactor, the former not.
@oberstet So non-async code won't be executed concurrently, because workers are using only single threads?
To run code concurrently in the sense of utilizing multiple cores (parallel), you must use threads or processes.
To run blocking code, that code must be run on a background thread, because you otherwise this blocks the event loop.
You can run multiple threads with an event loop on each - which you then must not block.
These statements are true in general (not specific to Crossbar.io, Autobahn, Twisted or even Python).
@oberstet Ok, I understand. But in case of blocking code, could you explain weird behaviour presented on gif? Why reactor return value of first call, then execute rest calls and finally return values instead of returning after each execution?
Sometimes there are 4 response after 4 sec. and last after 1 sec. more:
While you're sleep
-ing, the reactor can't do anything (including receive or send data that's ready). I'm not sure exactly what happens after that, but blocking the reactor in this way will definitely lead to bad/weird behavior, as you've discovered ;)
Description
I wanted to see how Crossbar deal with concurrent calls, so I added
sleep(1)
to my function and called it few times. I expected to get all responses after 1 second or one response every second in case of sequential execution. But actual behaviour astonished me: I get first response after 1s but other responses comes together after several seconds: I thought it can be Thruway issue, but using AutobahnPy I get same result:Version:
Steps to reproduce the issue
composer install
andcrossbar start
index_php.html
orindex_py.html