1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #!/usr/bin/env python3
# coding: utf-8
import random
import sys
import time
from threading import Thread
from threading import RLock
class cTraitement(Thread):
def __init__(self, mot, verrou, count=5):
super().__init__()
self.__mot=mot
self.__verrou=verrou
self.__count=count
# __init__()
def run(self):
super().run()
for i in range(self.__count):
with self.__verrou:
for l in self.__mot:
sys.stdout.write(l)
sys.stdout.flush()
# for
# with
time.sleep(random.randint(1, 60) / 100.0 + 0.2)
# for
# run()
# class cTraitement
verrou=RLock()
thread=tuple(cTraitement(x, verrou) for x in ("canard", "tortue", "lapin"))
for t in thread: t.start()
for t in thread: t.join() |