-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
DOCS: sync/async guide for sanic #2884
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2884 +/- ##
=========================================
Coverage 88.039% 88.039%
=========================================
Files 94 94
Lines 7433 7433
Branches 1283 1283
=========================================
Hits 6544 6544
Misses 622 622
Partials 267 267 ☔ View full report in Codecov by Sentry. |
def sync_print(text: str) -> None: | ||
rand_wait: int = randrange(0,5) | ||
time.sleep(rand_wait) | ||
print(f'{text} ({rand_wait}s)') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to take the delay as argument, rather than random which only adds to complexity here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be using it for consistency when I get to demonstrating the async/await versions. It adds complexity, but it's also a relatively simple path to show the impact of asynchronous code. I'm putting a pin in it, but I'd like your opinion when I've got the section complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should work with fixed delays similarly for both async and sync. I hope you have read https://trio.readthedocs.io/en/stable/tutorial.html which is a particularly good introduction to async issues (even when not using Trio).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tronic after sleeping on it, I'll revise to use the delay as an argument as you suggest. I don't think the complexity is inherently a problem with using random sleep times, but given how many people seem confused about async/await removing that from the process makes sense.
|
||
# Sync and Async in Sanic | ||
|
||
Sanic is an **asynchronous** framework - it is designed to be responsive to requests, which allows it to theoretically serve more requests, more quickly, than a synchronous framework. Other python asynchronous frameworks also share the same limitations when calling synchronous code, so the guide below will hopefully serve you if you end up in a situation using something other than Sanic in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sanic is an **asynchronous** framework - it is designed to be responsive to requests, which allows it to theoretically serve more requests, more quickly, than a synchronous framework. Other python asynchronous frameworks also share the same limitations when calling synchronous code, so the guide below will hopefully serve you if you end up in a situation using something other than Sanic in the future. | |
Sanic is an **asynchronous** framework—it is designed to be responsive to requests, which allows it to theoretically serve more requests, more quickly, than a synchronous framework. Other python asynchronous frameworks also share the same limitations when calling synchronous code, so the guide below will hopefully serve you if you end up in a situation using something other than Sanic in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, the typographer in me cannot overlook the mistake of a hyphen for a dash.
Can we get this finalized soon? |
|
||
Most human beings are not that interested in standing at the kettle, doing nothing until it boils, but with regular functions, that's what happens: Fill kettle. Boil. Use water to make drink. The operations happen in order, and you cannot do anything else until they complete. | ||
|
||
For someone making a drink, that seems like a waste of time. A person does not need to stand around and watch the kettle until it boils, instead, other things can be done. So the kettle is left to do its task, which by filling it and turning it on, we can consider to be scheduled, and when it is done, we can come back to it and make our hot drink. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is trying to talk to a wide audience but I feel its too dumbed down to be of use to someone that actually gets to the point of reading it. i think that most people that would be reading this would definitely have an understanding of asyncio
(though the motivation here could be something I've just not witnessed!)
I think you could deal with the concept in far fewer lines and do away with a noddy example, and defer more detail to https://docs.python.org/3/library/asyncio.html and go on to deal with sanic
specific detail instead ?
Much needed guide for explaining what happens when blocking calls are used in Sanic