Skip to content

Instantly share code, notes, and snippets.

@gtindo
Created October 29, 2019 10:31
Show Gist options
  • Save gtindo/ee8a60432a517a2e0526d4ae09f75786 to your computer and use it in GitHub Desktop.
Save gtindo/ee8a60432a517a2e0526d4ae09f75786 to your computer and use it in GitHub Desktop.
Worker thread is doing some task, logger thread log printed messages
from threading import Thread, Event
event = Event()
def worker(number, outputs, exit_code):
msg = "=== Table de mulitiplication par {}".format(number)
outputs.append(msg)
for i in range(1, number):
msg = "{} * {} = {}".format(i, number, i*number)
outputs.append(msg)
print(msg)
exit_code[0] = 0
def logger(outputs, exit_code):
log = []
i = 0
while 1:
n_log = len(log)
n_out = len(outputs)
if n_log < n_out:
print("log : {}".format(outputs[i]))
log.append(outputs)
i = i + 1
if exit_code[0] == 0 and n_log == n_out:
break
out = []
ex_code = [-1]
t1 = Thread(target=worker, args=(12, out, ex_code, ))
t2 = Thread(target=logger, args=(out, ex_code, ))
t1.start()
t2.start()
@Sanix-Darker
Copy link

Realy nice Trick !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment