Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active April 27, 2018 22:51
Show Gist options
  • Select an option

  • Save perrygeo/5e39922468958810b0f55c28cef0e15a to your computer and use it in GitHub Desktop.

Select an option

Save perrygeo/5e39922468958810b0f55c28cef0e15a to your computer and use it in GitHub Desktop.
import random
import time
import json
import threading
import click
def send_message(msg, async=True):
"""Logs the message to an output stream.
If async is true, use a background thread
Based on
http://sebastiandahlgren.se/2014/06/27/running-a-method-as-a-background-thread-in-python/
"""
def send(msg=msg):
try:
# Simulates a system where the IO could take up to 5 seconds
time.sleep(random.random() * 5)
print(f"Sending {msg}") # IO
except Exception:
pass # swallow any and all exceptions
if async:
thread = threading.Thread(target=send)
thread.daemon = True
thread.start()
else:
send()
@click.command()
@click.option('--async/--sync', is_flag=True, default=True)
def main(async):
click.echo("Async..." if async else "Sync...")
for i in range(10):
send_message(json.dumps({'a': i}), async=async)
print("I'm done logging, start the heavy processing")
time.sleep(5)
print("I'm done processing")
if __name__ == "__main__":
main()
@perrygeo

perrygeo commented Apr 27, 2018 •

Copy link
Copy Markdown
Author

If the main process exits before the threads, any remaining threads are "orphaned" and may not complete. To avoid this, set thread.daemon = False to allow all threads to run until completion.

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