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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
from threading import Thread
from multiprocessing import Pool
import time
def a(time_to_sleep,message):
"""
:param time_to_sleep: (Double) the time in sec
:param message: (String) the message to return
"""
t1 = time.time()
time.sleep(time_to_sleep)
print(message, time.time()-t1)
class MonThread(Thread):
def __init__(self, time_to_sleep, message):
super(MonThread, self).__init__()
self.time_to_sleep = time_to_sleep
self.message = message
def run(self):
t1 = time.time()
time.sleep(self.time_to_sleep)
print(self.message, time.time() - t1)
if __name__ == '__main__':
thread1=Thread(target=a,args=(3, "thread1 termine"))
thread2=Thread(target=a,args=( 6, "thread2 termine"))
thread1.start()
thread2.start()
print("le thread principal continue")
thread1.join()
print('j attend la fin du thread 1')
thread2.join()
print('j attend a fin du thread 2')
print("par classe")
thread1= MonThread(2, "thead3")
thread2 = MonThread(4, "thread4")
thread2.start()
thread1.start()
thread2.join()
thread1.join()
print("fin")
print("multiprocess")
# La fonction Pool est une méthode simple pour faire plusieurs fois la même fonction avec des arguments différents
with Pool(2)as p:
p.starmap(a, [(1, "01"), (2, '02')]) |
Partager