Skip to content

Instantly share code, notes, and snippets.

@perrygeo
Last active April 27, 2018 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save perrygeo/5e39922468958810b0f55c28cef0e15a to your computer and use it in GitHub Desktop.
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
Copy link
Author

perrygeo commented Apr 27, 2018

⭆ time python send_message.py
Async...
I'm done logging, start the heavy processing
Sending {"a": 0}
Sending {"a": 6}
Sending {"a": 9}
Sending {"a": 3}
Sending {"a": 5}
Sending {"a": 8}
Sending {"a": 7}
Sending {"a": 2}
Sending {"a": 1}
Sending {"a": 4}
I'm done processing

real	0m5.078s
user	0m0.058s
sys	0m0.019s


⭆ time python send_message.py --sync
Sync...
Sending {"a": 0}
Sending {"a": 1}
Sending {"a": 2}
Sending {"a": 3}
Sending {"a": 4}
Sending {"a": 5}
Sending {"a": 6}
Sending {"a": 7}
Sending {"a": 8}
Sending {"a": 9}
I'm done logging, start the heavy processing
I'm done processing

real	0m21.524s
user	0m0.058s
sys	0m0.019s

@perrygeo
Copy link
Author

perrygeo commented Apr 27, 2018

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