Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to run same function on multiple threads in pyhton

import multiprocessing

def worker(num):
    """ Worker procedure
    """
    print('Worker:', str(num))

# Mind the "if" instruction!
if __name__ == '__main__':
    jobs = [] # list of jobs
    jobs_num = 5 # number of workers
    for i in range(jobs_num):
        # Declare a new process and pass arguments to it
        p1 = multiprocessing.Process(target=worker, args=(i,))
        jobs.append(p1)
        # Declare a new process and pass arguments to it
        p2 = multiprocessing.Process(target=worker, args=(i+10,))
        jobs.append(p2)
        p1.start() # starting workers
        p2.start() # starting workers
Comment

PREVIOUS NEXT
Code Example
Python :: if main python 
Python :: drop all unnamed columns pandas 
Python :: get_dummies 
Python :: matplotlib animate 
Python :: python getters and setters 
Python :: Python program to print even numbers in a list 
Python :: add column to start of dataframe pandas 
Python :: startapp django 
Python :: python replace line in file 
Python :: creating empty set and append python 
Python :: how to power in python 
Python :: python sort the values in a dictionaryi 
Python :: remove tab space from string in python 
Python :: randomly shuffle pandas dataframe 
Python :: python is inf 
Python :: pandas count nans in column 
Python :: pyqt button clicked connect 
Python :: how to convert adjacency list to adjacency matrix 
Python :: numpy rolling average 
Python :: seaborn pink green color palette python 
Python :: timer 1hr 
Python :: Custom x, y-ticks using plt 
Python :: php echo shorthand 
Python :: python message from teams 
Python :: find highest correlation pairs pandas 
Python :: add value to dictionary python 
Python :: how to print keys and values of dictionary together in python? 
Python :: python compare floats 
Python :: Iterate string 2 characters at a time in python 
Python :: django model form 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =