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 :: auto instagram login 
Python :: install matplotlib on nvidia jetson nx 
Python :: code.org void loops 
Python :: how to check if a column exists before alter the table 
Python :: how to store svgs in django image field with SVGAndImageFormField 
Python :: scipy get frequencies of image 
Python :: matplotlib FiveThirtyEight creating a signature 
Python :: Python regex emailadres no jpg 
Python :: python tokenize sentence italian spacy 
Python :: how to accept invalidfileexception in python 
Python :: get element tag name beautfulsoup 
Python :: pytorch rolling window 
Python :: How can I use Apache Spark with notebook in Anaconda 
Python :: how to implement nfa in python 
Python :: form list of filename get the filename with highest num pythn 
Python :: how to run function when file is modified python 
Python :: ENUM AS STRING GODOT 
Python :: can we put the object as value in a dictionary in python* 
Python :: raise httperror(req.full_url, code, msg, hdrs, fp) urllib.error.httperror: http error 429: too many requests 
Python :: This code is supposed to display "2 "2 + 2 = 4"" on the screen, but there is an error. Find the error in the code and fix it, so that the output is correct. 
Python :: python regex type hint 
Python :: Specifying your data type 
Python :: fill missing values with dict 
Python :: add Elements to Python list Using extend() method 
Python :: unique character 01 
Python :: python replace every space, dash and parentheses into underscore 
Python :: Sequential Execution EC2 
Python :: grab element based on text from html page in python 
Python :: run all pycharm jupyter notebook 
Python :: Python NumPy asfarray Function Syntax 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =