Created
January 9, 2022 02:50
-
-
Save rom1504/ecf47f8c1cc976b58f1283434d29f826 to your computer and use it in GitHub Desktop.
stoppable_process.py
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
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