-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #335 from r0fls/remove-loop
remove loop as argument and update examples
- Loading branch information
Showing
10 changed files
with
143 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from sanic import Sanic | ||
from sanic.response import json | ||
|
||
import asyncio | ||
import aiohttp | ||
|
||
app = Sanic(__name__) | ||
|
||
sem = None | ||
|
||
def init(sanic, loop): | ||
global sem | ||
CONCURRENCY_PER_WORKER = 4 | ||
sem = asyncio.Semaphore(CONCURRENCY_PER_WORKER, loop=loop) | ||
|
||
async def bounded_fetch(session, url): | ||
""" | ||
Use session object to perform 'get' request on url | ||
""" | ||
async with sem, session.get(url) as response: | ||
return await response.json() | ||
|
||
|
||
@app.route("/") | ||
async def test(request): | ||
""" | ||
Download and serve example JSON | ||
""" | ||
url = "https://api.github.com/repos/channelcat/sanic" | ||
|
||
async with aiohttp.ClientSession() as session: | ||
response = await bounded_fetch(session, url) | ||
return json(response) | ||
|
||
|
||
app.run(host="0.0.0.0", port=8000, workers=2, before_start=init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.