-
Notifications
You must be signed in to change notification settings - Fork 11
Create 4. Example to transition to a scene #6
base: master
Are you sure you want to change the base?
Conversation
Loop through a set of scenes. SetCurrentSceneRequest makes the transition to the named scene.
Hey, thanks for the PR! However, I wanted the examples to be fundamental, that's why there're only 3 of them. They show how to make requests, how to await for events and how it's possible to utilize This example very closely resembles example #2, it just uses a different request. We surely can't afford making an example for every request, because that
Anyways, I want to warn you against using blocking calls (such as Whenever you If you want the async to work, you need to get rid of all blocking calls in your code. Most blocking calls are natually related to Input/Output (like reading a socket would block your program until you receive some data over the network). The "io" in Now, your blocking call is time.sleep(5) the event loop can't get control. The whole program hangs because it's single-threaded. What you do want to do though is this: await asyncio.sleep(5)
|
I understand you comments about the rationale for the examples. You disregard the PR.
Instead, maybe I could figure out how to improve the documentation. You seem to understand well the connection between your package on the protocol doc. As someone just taping into it I find that connection hard to follow myself. Perhaps I am too limited by my ‘beginner level’ python skills.
As for the time.sleep being a blocking call. Great point. This is my first time working with async features in python … and it has been a long time, 15+ years, since doing it in Java . It would not have been part of my ‘production’ application, but good to know. For my own education I did change my example to use await asyncio.sleep(5) and it worked fine.
Thanks again,
Terry
… On Feb 18, 2018, at 3:36 PM, Kirill Mysnik ***@***.***> wrote:
Hey, thanks for the PR!
However, I wanted the examples to be fundamental, that's why there're only 3 of them. They show how to make requests, how to await for events and how it's possible to utilize asyncio API to control the flow of the obs-ws-rc-powered application for, say, interrupting event loop on a timeout.
This example very closely resembles example #2 <https://github.com/KirillMysnik/obs-ws-rc/blob/master/examples/2.%20Make%20requests/make_requests.py>, it just uses a different request. We surely can't afford making an example for every request, because that
Is not neccessary as this purpose is served by the docs;
Would make keeping the protocol up-to-date more difficult as any changes would have had to be reflected in the examples as well.
Anyways, I want to warn you against using blocking calls (such as time.sleep) in asyncio-based applications. That defeats the whole idea of the async. Asynchronous applications are opposed to threaded applications. asyncio allows you to do multiple tasks concurrently with only one thread.
Whenever you await for something, you tell the event loop (the one that you initialize with asyncio.get_event_loop()) that it's free to switch to other tasks. When your await target is ready, event loop will switch back to you as soon as possible, but no sooner than some other task releases control with its own await call (or if there are no other unfinished tasks). These calls are not blocking in the classic sense as the thread is not blocked. It's just that the context gets switched to the other part of the code.
If you want the async to work, you need to get rid of all blocking calls in your code. Most blocking calls are natually related to Input/Output (like reading a socket would block your program until you receive some data over the network). The "io" in asyncio means exactly that: this packages does not only provide the event loop. It provides API to work with sockets, files and streams that you can use to build an async application.
Now, your blocking call is time.sleep that you use to delay scene switching. When you do
time.sleep(5)
the event loop can't get control. The whole program hangs because it's single-threaded. What you do want to do though is this:
await asyncio.sleep(5)
asyncio.sleep functions similarly to time.sleep but instead of blocking it just passes control to the async event loop so it may switch to other tasks (if any) while your task is waiting. After 5 seconds have passed, the event loop will switch back to you.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#6 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AgLJ9ZTzt7qZT6QivRJOt7QnzOxlCu7cks5tWImzgaJpZM4SJVFM>.
|
Loop through a set of scenes. SetCurrentSceneRequest makes the transition to the named scene.