How to call the same function using multiple threads in python?

10,519

Long story short : No, because of the Global Interpreter Lock.

But you might want to consider using processes instead of threads, which appear to be your real need on your application (as far as one can understand from your explanations), since it seems you do not need to make use of a shared memory space.

A (very) simple example:

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

This example will give you the following output:

$ python multiprocessing_example.py

Worker: 0
Worker: 10
Worker: 1
Worker: 11
Worker: 2
Worker: 12
Worker: 3
Worker: 13
Worker: 4
Worker: 14

To make good use of multiples processes, I recommend you learn a little about the documentation of the module, the GIL, the differences between threads and processes and, especially, how it can speed up or limit what you want to do and how.

For example, going multiprocessing will not help you if your bottleneck is I/O, but will help if your processing is limited by your CPU.

Share:
10,519
cryptoKay
Author by

cryptoKay

Updated on June 05, 2022

Comments

  • cryptoKay
    cryptoKay almost 2 years

    I am working on a signal processing application in python. I want call the same function using multiple threads in python, to achieve concurrent execution is it possible to do it? If so how?