3

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?

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

7 Comments

Dear SteveJ, thank you for your help. I guess they don't really work completely in parallel so I will be missing part of the data if I run them together.
There are ways to share data -- plenty of info out there on the web. Also, you can sync the processes, depending on your needs. I would do a search for long running calculations in Python and get some tips from there.
@Mariano If this answered your question, please mark it as such. If not, please clarify and I'll attempt to provide a better answer.
Dear SteveJ, Yes It does, Clearly I need to find another solution, Even if I would just copy and paste the same function and change the name and run them in parallel still it will follow First , Second order so I will clearly not get all the data, thank you for your help.
@Mariano ; Then you want to select the check next to "answer". This both gives the responder credit for helping and tells others that they don't need to review the question since it already has an answer. Not asking you to do it for this one, but if you find an answer useful (in the future), it is also customary to up-vote it. It is the only pay that folks get for their time spent helping others.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.