Skip to content

Instantly share code, notes, and snippets.

@justinholmes
Last active August 1, 2018 09:20
Show Gist options
  • Save justinholmes/642bcef8b6531ece74a5ce353a6f8f2f to your computer and use it in GitHub Desktop.
Save justinholmes/642bcef8b6531ece74a5ce353a6f8f2f to your computer and use it in GitHub Desktop.
Python background threading and threadpool executor example
import threading
from concurrent.futures import ThreadPoolExecutor
from time import sleep
class ProcessingBackgroundThread:
def __init__(self):
thread = threading.Thread(target=self.process)
thread.daemon = True
thread.start()
def process(self):
print("Doing work")
class ProcessingThreadPool():
def __init__(self):
with ThreadPoolExecutor(max_workers=2) as executor:
future = executor.submit(self.return_after_2_secs, ("hello"))
print(future.done())
sleep(5)
print(future.done())
print(future.result())
def return_after_2_secs(self, message):
sleep(2)
return message
ProcessingBackgroundThread()
ProcessingThreadPool()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment