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

DOCS: sync/async guide for sanic #2884

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

sjsadowski
Copy link
Contributor

Much needed guide for explaining what happens when blocking calls are used in Sanic

Copy link

codecov bot commented Dec 25, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b651636) 88.039% compared to head (fd65d41) 88.039%.

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.
📢 Have feedback on the report? Share it here.

Comment on lines +27 to +30
def sync_print(text: str) -> None:
rand_wait: int = randrange(0,5)
time.sleep(rand_wait)
print(f'{text} ({rand_wait}s)')
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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).

Copy link
Contributor Author

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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** frameworkit 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.

Copy link
Member

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.

@ahopkins
Copy link
Member

ahopkins commented Apr 1, 2024

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.

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 ?

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

Successfully merging this pull request may close these issues.

4 participants