Created
January 13, 2022 02:38
-
-
Save lwzm/ba2f2727600291f6eb59cf6e93afa903 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python3 | |
from datetime import datetime | |
from sys import argv, stderr | |
from threading import Thread, Lock | |
from time import sleep | |
lock = Lock() | |
class g: | |
stop = False | |
n = 0 | |
def do(seq): | |
"implement this" | |
def loop(seq): | |
while True: | |
if g.stop: | |
break | |
do(seq) | |
with lock: | |
g.n += 1 | |
def main(n): | |
def log(x): | |
print(x, file=stderr, flush=True) | |
ths = [Thread(target=loop, args=(i,)) for i in range(n)] | |
for th in ths: | |
th.start() | |
log(th) | |
while True: | |
try: | |
sleep(1) | |
with lock: | |
log(g.n) | |
g.n = 0 | |
except (KeyboardInterrupt, Exception) as e: | |
g.stop = True | |
for th in ths: | |
th.join() | |
raise | |
if __name__ == '__main__': | |
main(int(argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment