Last active
August 1, 2018 09:20
-
-
Save justinholmes/642bcef8b6531ece74a5ce353a6f8f2f to your computer and use it in GitHub Desktop.
Python background threading and threadpool executor example
This file contains 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 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