Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Create 4. Example to transition to a scene #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

avmaint
Copy link

@avmaint avmaint commented Feb 17, 2018

Loop through a set of scenes. SetCurrentSceneRequest makes the transition to the named scene.

Loop through a set of scenes. SetCurrentSceneRequest makes the transition to the named scene.
@KirillMysnik
Copy link
Owner

KirillMysnik commented Feb 18, 2018

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, it just uses a different request. We surely can't afford making an example for every request, because that

  1. Is not necessary as this purpose is served by the docs;
  2. 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.

@avmaint
Copy link
Author

avmaint commented Feb 20, 2018 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants