'Why is it python multiprocessing doesn't work if I add start_candles_stream() function from iqoptionapi in a function to be processed?

I am trying to call a function which contains start_candles_stream() function from the iqoptionapi but it fails or I can say it freezes when the code reach the start_candles_stream() function (function which returns live market prices). If I try to use the simple task inside it works perfectly. Below is my working code:

from iqoptionapi.stable_api import IQ_Option
import multiprocessing

user = "[email protected]"
password = "your_password"


API = IQ_Option(user,password)
if API.connect():
    print("Connected Successfully")

#function which is working now without the start_candles_stream() include
def currentPrice(asset,timeframe,type="open"):
    print(f'{type} price for {asset} with {timeframe} seconds timeframe')

# function I am calling
def main():
    multiprocessing.Process(target=currentPrice, args=("EURUSD-OTC",60,"close")).start()

if __name__ == '__main__':
    main()

The above code is working fine without start_candles_stream() but if I replace the currentPrice() function with the following code it becomes the problem:

def currentPrice(asset,timeframe,type="open"):
    API.start_candles_stream(asset,timeframe,1) ##Problem starts here
    candles = API.get_realtime_candles(asset,timeframe)
    for key,values in candles.items():
        print(candles[key][type])

If I call the currentPrice() out of multiprocessing it works fine but I want to use multiprocessing because i will be using a for loop later. The results I am getting are as follows after pressing Ctrl&C due to freezing without action:

user@user:~/IQBOT$ python3 test.py
Connected Successfully
^CError in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3.8/multiprocessing/popen_fork.py", line 27, in poll
    pid, sts = os.waitpid(self.pid, flag)
KeyboardInterrupt
ERROR:root:**error** get_candles need reconnect
user@user:~/IQBOT$ 1.043546

Why is it python multiprocessing doesn't work if I add start_candles_stream() function from iqoptionapi in a function to be processed? If anyone knows a better alternative solution to this please help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source