Python language: main applications in Data Science, Artificial Intelligence & Machine learning.

Опубликовано: 27 Октябрь 2024
на канале: Somenath Roy Choudhury (src7bppimt)
199
like

import os
import threading
We use os.getpid() function to get ID of current process.
print("ID of process running main program: {}".format(os.getpid()))
print name of main thread
print("Main thread name: {}".format(threading.main_thread().name))

#before multi-threading
from time import sleep, perf_counter

def task():
print('Starting a task...')
sleep(1)
print('done')


start_time = perf_counter()

task()
task()

end_time = perf_counter()
print(end_time)

from time import sleep, perf_counter
from threading import Thread

def task():
print('Starting a task...')
sleep(1)
print('done')

start_time = perf_counter()

create two new threads
t1 = Thread(target=task)
t2 = Thread(target=task)

start the threads
t1.start()
t2.start()

wait for the threads to complete
t1.join()
t2.join()

end_time = perf_counter()

print(f'It took {end_time- start_time: 0.2f} second(s) to complete.')