Horje
# multithreading for optimal use of CPU Code Example
# multithreading for optimal use of CPU
# multithreading for optimal use of CPU
import time
import threading

def calc_square(numbers):
    print("calculate square numbers")
    for n in numbers:
        time.sleep(1) # CPU becomes idle for 1 second
        print('square:',n*n)

def calc_cube(numbers):
    print("calculate cube of numbers ")
    for n in numbers:
        time.sleep(1) # CPU becomes idle for 1 second
        print('cube:',n*n*n)

arr = [2,3,8,9]

t = time.time()

t1= threading.Thread(target=calc_square, args=(arr,))
t2= threading.Thread(target=calc_cube, args=(arr,))

t1.start()
t2.start()

t1.join()
t2.join()

print("DONE IN : ",time.time()-t)

# Multithreading allows the execution of multiple parts of a program at the same time. This can be crucial at times when multiple actions that happen simultaneously needs to be handled. Else, they can be used to execute another piece of code(function) while the CPU is idle.




Python

Related
\n appears in json dump Code Example \n appears in json dump Code Example
Return a Series containing counts of unique values. Code Example Return a Series containing counts of unique values. Code Example
write str in a formal way in python Code Example write str in a formal way in python Code Example
alphabeticallly Code Example alphabeticallly Code Example
python install progressbar Code Example python install progressbar Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7