Created
February 25, 2016 07:49
-
-
Save klang/e93ce303a1611a38069b to your computer and use it in GitHub Desktop.
a control loop keeping several threads alive
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
virtualenv -p $(which python3) venv | |
source venv/bin/activate | |
(venv)[klang@ergates workers]$ python -i tester_run.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
looper:08:44:15 - limited-2 initializing | |
looper:08:44:15 - limited-5 initializing | |
looper:08:44:16 - limited-4 initializing | |
looper:08:44:16 - another-2 initializing | |
looper:08:44:16 - another-1 initializing | |
looper:08:44:16 - limited-3 initializing | |
looper:08:44:17 - limited-1 initializing | |
looper:08:44:18 - limited-2 starting up | |
limited-2:08:44:18 - 1 | |
looper:08:44:18 - limited-5 starting up | |
limited-5:08:44:18 - 1 | |
looper:08:44:18 - limited-4 starting up | |
limited-4:08:44:18 - 1 | |
looper:08:44:19 - another-2 starting up | |
another-2:08:44:19 - 1 | |
looper:08:44:19 - another-1 starting up | |
another-1:08:44:19 - 1 | |
looper:08:44:19 - limited-3 starting up | |
limited-3:08:44:19 - 1 | |
looper:08:44:19 - limited-1 starting up | |
limited-1:08:44:19 - 1 | |
another-2:08:44:21 - 2 | |
another-1:08:44:21 - 2 | |
another-2:08:44:23 - 3 | |
limited-2:08:44:23 - 2 | |
another-1:08:44:23 - 3 | |
limited-5:08:44:23 - 2 | |
limited-4:08:44:23 - 2 | |
limited-3:08:44:24 - 2 | |
limited-1:08:44:24 - 2 | |
another-2:08:44:25 - 4 | |
another-1:08:44:25 - 4 | |
another-2:08:44:27 - 5 | |
another-1:08:44:27 - 5 | |
limited-2:08:44:28 - 3 | |
limited-5:08:44:28 - 3 | |
limited-4:08:44:28 - 3 | |
limited-3:08:44:29 - 3 | |
limited-1:08:44:29 - 3 | |
looper:08:44:30 - another-2 initializing | |
looper:08:44:30 - another-1 initializing | |
looper:08:44:33 - another-2 starting up | |
another-2:08:44:33 - 1 | |
looper:08:44:33 - another-1 starting up | |
another-1:08:44:33 - 1 | |
limited-2:08:44:33 - 4 | |
limited-5:08:44:33 - 4 | |
limited-4:08:44:33 - 4 | |
limited-3:08:44:34 - 4 | |
limited-1:08:44:34 - 4 | |
another-2:08:44:35 - 2 | |
another-1:08:44:35 - 2 | |
another-2:08:44:37 - 3 | |
another-1:08:44:37 - 3 | |
limited-2:08:44:38 - 5 | |
limited-5:08:44:38 - 5 | |
limited-4:08:44:38 - 5 | |
another-2:08:44:39 - 4 | |
another-1:08:44:39 - 4 | |
limited-3:08:44:39 - 5 | |
limited-1:08:44:39 - 5 | |
another-2:08:44:41 - 5 | |
another-1:08:44:41 - 5 | |
looper:08:44:44 - another-2 initializing | |
looper:08:44:44 - another-1 initializing | |
looper:08:44:46 - limited-2 initializing | |
looper:08:44:46 - limited-5 initializing | |
looper:08:44:46 - limited-4 initializing | |
looper:08:44:46 - another-2 starting up | |
another-2:08:44:46 - 1 | |
looper:08:44:47 - another-1 starting up | |
another-1:08:44:47 - 1 | |
looper:08:44:47 - limited-3 initializing | |
looper:08:44:47 - limited-1 initializing | |
another-2:08:44:48 - 2 | |
looper:08:44:48 - limited-2 starting up | |
limited-2:08:44:48 - 1 | |
another-1:08:44:49 - 2 | |
looper:08:44:49 - limited-5 starting up | |
limited-5:08:44:49 - 1 | |
looper:08:44:49 - limited-4 starting up | |
limited-4:08:44:49 - 1 | |
looper:08:44:50 - limited-3 starting up | |
limited-3:08:44:50 - 1 | |
looper:08:44:50 - limited-1 starting up | |
limited-1:08:44:50 - 1 | |
another-2:08:44:50 - 3 | |
another-1:08:44:51 - 3 |
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 logging, time | |
def startLimited(): | |
count = 1 | |
for i in range(5): | |
logging.info(time.strftime("%H:%M:%S") + " - " + str(count)) | |
count = count + 1 | |
time.sleep(3) | |
def startAnother(): | |
count = 1 | |
for i in range(5): | |
logging.info(time.strftime("%H:%M:%S") + " - " + str(count)) | |
count = count + 1 | |
time.sleep(2) | |
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,time,logging, tester | |
logging.basicConfig(filename='tester.log',level=logging.INFO, format="%(threadName)s:%(message)s") | |
threads = {"limited-1": None, | |
"limited-2": None, | |
"limited-3": None, | |
"limited-4": None, | |
"limited-5": None, | |
"another-1": None, | |
"another-2": None} | |
def startThread(name): | |
threadType = name.split("-")[0] | |
newThread = None | |
if threadType == "limited": | |
newThread = threading.Thread(target=tester.startLimited, name=name, daemon=True) | |
elif threadType == "another": | |
newThread = threading.Thread(target=tester.startAnother, name=name, daemon=True) | |
return newThread | |
def loopylooper(): | |
while 1: | |
for name,thread in threads.items(): | |
# if the thread isn't initialized yet or has died | |
if thread == None or thread._started.isSet() and not thread.isAlive(): | |
logging.info(time.strftime("%H:%M:%S") + " - " + name + " initializing") | |
threads[name] = startThread(name) | |
# if the thread has never been started, start it. | |
if not thread == None and not thread._started.isSet() and not thread.isAlive(): | |
logging.info(time.strftime("%H:%M:%S") + " - " + name + " starting up") | |
thread.start() | |
time.sleep(0.25) | |
time.sleep(1) | |
looper=threading.Thread(target=loopylooper, name="looper", daemon=True) | |
looper.start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment