Home > database >  2 differeent objectt instance printing same result of first instance
2 differeent objectt instance printing same result of first instance

Time:03-16

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.

CodePudding user response:

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')

CodePudding user response:

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

CodePudding user response:

Thanks my friends, ive 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()
  • Related