Last active
December 12, 2019 08:03
-
-
Save CjS77/81e1dfd6890c77fcde01fb932a9802bc to your computer and use it in GitHub Desktop.
Python futures demo
This file contains hidden or 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
import urllib.request | |
import concurrent.futures | |
import time | |
# Takes a url, downloads the content, then does something with it. Returns | |
# True if everything went fine | |
def process_web_request(url): | |
try: | |
with urllib.request.urlopen(url) as response: | |
html = response.read() | |
if html: | |
do_something_with(html) | |
return True | |
else: return False | |
except Exception as e: | |
print("Error %s" % (e,)) | |
return False | |
def do_something_with(html): | |
# Do something useful here. It won't block the main thread | |
print("Got data. Doing stuff with it") | |
time.sleep(5) | |
print(html) | |
def main(): | |
executor = concurrent.futures.ThreadPoolExecutor(max_workers=2) | |
# Run "process_web_request" on a background thread. You'll see dots being printed to prove | |
# that other stuff carries on in the meantime | |
executor.submit(process_web_request, 'https://portfolio.nimbustechnologies.co.za/api') | |
# Print dots while we wait for | |
for i in range(0,15): | |
time.sleep(0.5) | |
print(".") | |
executor.shutdown(wait=True) | |
print("Done") | |
### You should see a few dots get printed, then you'll see the message that the website was fetched. | |
### After 5 seconds of hard work, (with more dots printed), you'll see the website content printed | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment