Skip to content

Instantly share code, notes, and snippets.

@rom1504
Created January 9, 2022 02:50
Show Gist options
  • Save rom1504/ecf47f8c1cc976b58f1283434d29f826 to your computer and use it in GitHub Desktop.
Save rom1504/ecf47f8c1cc976b58f1283434d29f826 to your computer and use it in GitHub Desktop.
stoppable_process.py
from multiprocessing import Process, Queue
import queue
import time
class StoppableProcess(Process):
def __init__(self, lol):
super().__init__()
self.lol = lol
self.q = Queue()
def run(self):
print(self.lol)
while True:
time.sleep(1)
print("hi")
try:
self.q.get(False)
self.q.close()
return
except queue.Empty as _:
pass
def join(self):
print("time to stop")
self.q.put("stop")
super().join()
self.q.close()
if __name__ == "__main__":
#time.sleep(5)
a = LoggerProcess("a")
a.start()
time.sleep(5)
print("oh well")
a.join()
time.sleep(5)
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment