0% found this document useful (0 votes)
17 views2 pages

Multi Threading

Uploaded by

Dennis Ivan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

Multi Threading

Uploaded by

Dennis Ivan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import logging

import threading
import time
import string

logging.basicConfig(level=logging.DEBUG,format='[%(levelname)s] (%(threadName)-
10s) %(message)s',)
start = time.perf_counter()

class Work:
def __init__(self):
self.run=0

def worker_1(self):
logging.debug('Worker 1 starts')
i = 1
while self.run < 4:
print(f'Worker 1 count: {i} ')
i += 1
time.sleep(1)
if i == 5:
self.run += 1
logging.debug("Worker 1 exits")

def worker_2(self):
letters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P
","Q","R","S","T","U","V","W","X","Y","Z"]
while self.run !=1:
continue
logging.debug('Worker 2 starts')
for i in letters:
print(f'Worker 2 print: {i} ')
if i == "L":
self.run += 1
time.sleep(1)
self.run += 1
logging.debug('Worker 2 exits')

def worker_3(self):
while self.run != 2:
continue
logging.debug('Worker 3 starts')
for i in range(15,-1,-1):
print(f'Worker 3 count: {i} ')
time.sleep(1)
self.run += 1
logging.debug("Worker 3 exits")

workers = Work()
thread1 = threading.Thread(target=workers.worker_1)
thread2 = threading.Thread(target=workers.worker_2)
thread3 = threading.Thread(target=workers.worker_3)

thread1.start()
thread2.start()
thread3.start()

thread1.join()
thread2.join()
thread3.join()

finish= time.perf_counter()
print(f'{round(finish-start, 2)} seconds')

You might also like