Mariano
Mariano

Reputation: 395

Running same function in parallel Python

I have created a function to gather cryptocurrency prices.

What I want to do is to run the function to gather order book prices for different cryptos in parallel.

The function is basically the same, the only thing changing is the crypto.

example:

def gather_prices(pair):
    get_prices = order_book(pair)

Since I am getting real time prices, I want to run the same function in parallel for different cryptos.

I see that I could use:

from multiprocessing import Process

def func1()
def func2()

if __name__ == '__main__':
Process(target=func1).start()
Process(target=func2).start()

My question is:

Will it work if the function is the same one?

rather than two different functions?

Upvotes: 3

Views: 5887

Answers (1)

SteveJ
SteveJ

Reputation: 3313

Easy enough to test;

from multiprocessing import Process
import time

def func1(message: str, sleep: int):
    while True:
        print(message)
        time.sleep(sleep)


if __name__ == '__main__':
    first = Process(target=func1, args=("First Call", 1)).start()
    second = Process(target=func1, args=("Second Call", 3)).start()

First Call
Second Call
First Call
First Call
First Call
Second Call
First Call
First Call
Second Call
First Call
First Call
First Call
Second Call
First Call
First Call

Upvotes: 4

Related Questions