'2 differeent objectt instance printing same result of first instance

from iqoptionapi.stable_api import IQ_Option

def connect_to_iq():


    API = IQ_Option('[email protected]','example01')
    
    API.connect()
    
    
    API_2 = IQ_Option('[email protected]','example02')
    
    API_2.connect()
    
    
    print(API.get_balance())
    
    print(API_2.get_balance())
  
connect_to_iq()
  
    # Make the same result i dont know why.

Account One Balance is 10036 Account Two Balance is 10000

But what is prints is:

10036 10036 I am not able to figure out why this is happening.



Solution 1:[1]

did you try giving your function email and pass parameters ?

def connect_to_iq(email, password):
    API = IQ_Option(email, pass)
    API.connect()
    return API.get_balance()

connect_to_iq('[email protected]','example01')
connect_to_iq('[email protected]','example02')

Solution 2:[2]

I've looked at the library, it holds state (ssid == session id I think) as global referenced variables. One solution I could think of is to instantiate the library per thread process.

That means import the library inline.

class ApiProcess(multiprocessing.Process):
    api = None

    def __init__(self, email, password):
        super().__init__()

        # Set parameters
        self.email = email
        self.password = password

    def run(self):
        # Run
        # Import the library as it will instantiate it, but this time per thread
        from iqoptionapi.stable_api import IQ_Option
        self.api = IQ_Option(self.email, self.password)
        self.api.connect()
        print(self.api.get_balance())

if __name__ == '__main__':
    process1 = ApiProcess("[email protected]", "example01")
    process2 = ApiProcess("[email protected]", "example02")

    process1.start()
    process2.start()

Do note, I didn't test this. It might not work, but from my understanding it should work.

EDIT: Changed to multiprocessing, not mutithreading as threads share memory

Solution 3:[3]

I've solved the problem with Multiprocess.

from iqoptionapi.stable_api import IQ_Option
from multiprocessing import Process

def conectar_iq(email,senha):

    API = IQ_Option(str(email),str(senha))
    
    API.connect()
    
    print(API.get_balance())
    

if __name__ == '__main__':

    processar = Process(target=conectar_iq, args=('[email protected]','example01',))
    process_two = Process(target=conectar_iq, args=('[email protected]','example02',))
    processar.start()
    process_two.start()

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 yxxhixx
Solution 2
Solution 3 Jeremy Caney