Skip to content

Instantly share code, notes, and snippets.

@1st
Created December 4, 2014 07:53
Show Gist options
  • Save 1st/c22d676ec59703ce23e9 to your computer and use it in GitHub Desktop.
Save 1st/c22d676ec59703ce23e9 to your computer and use it in GitHub Desktop.
"""
Demonstration how to execute Python code parallel with `yield` keyword.
Need Python 3.3 or above to use "return" in generator function.
This experiment is a step to implement Node.JS-like parallelism into Python web framework WebPages <https://github.com/webpages/webpages>
"""
def query_db():
return "result from database"
def operation_with_file(filename):
return "file content"
def render(template, data):
return template.format(data)
def request_handler():
filename = "hello.html"
template = yield operation_with_file(filename)
data = yield query_db()
return render(template, data)
for result in request_handler():
pass
@1st
Copy link
Author

1st commented Feb 28, 2025

Seems 11 years ago I thought about async abilities in Python :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment