Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python - multiprocessing

import time
from multiprocessing import Process

# My functions (threads)
def my_func_1():...
def my_func_2():...

# Single calculation  
start = time.time()
my_func_1()
my_func_2()
print(f'Single thread total time: {time.time() - start}')

# Processes
process = Process(target=my_func_1)
process2 = Process(target=my_func_2)
process.start()
process2.start()

start = time.time() # Start the two processes

process.join()      # Wait till processes finish
process2.join()

print(f'Two thread total time: {time.time() - start}')
Comment

Multiprocessing in Python

import multiprocessing
  
# empty list with global scope
result = []
  
def square_list(mylist):
    """
    function to square a given list
    """
    global result
    # append squares of mylist to global list result
    for num in mylist:
        result.append(num * num)
    # print global list result
    print("Result(in process p1): {}".format(result))
  
if __name__ == "__main__":
    # input list
    mylist = [1,2,3,4]
  
    # creating new process
    p1 = multiprocessing.Process(target=square_list, args=(mylist,))
    # starting process
    p1.start()
    # wait until process is finished
    p1.join()
  
    # print global result list
    print("Result(in main program): {}".format(result))
Comment

PREVIOUS NEXT
Code Example
Python :: TypeError: can only concatenate str (not "list") to str 
Python :: scikit decision tree regressor 
Python :: python sort array by key 
Python :: append multiple elements python 
Python :: django datefield year only 
Python :: python sort array by lambda 
Python :: pandas 
Python :: how to find duplicates in pandas 
Python :: matplotlib get padding from bbox 
Python :: how to import a module from a different directory in python 
Python :: pytesseract.image_to 
Python :: id() python 
Python :: Python How To Convert a String to Variable Name 
Python :: python re 
Python :: pd.explode 
Python :: how to read frame width of video in cv2 
Python :: if then else python 
Python :: match in python 
Python :: np minimum of array 
Python :: average python 
Python :: del en python 
Python :: python symbol 
Python :: set default dictionary in python 
Python :: how to join two tuples in python 
Python :: how to make a calculator 
Python :: django model queries 
Python :: função find python 
Python :: python iterate through list 
Python :: parse receipt python 
Python :: 1024x768 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =