[Python] attempting to execute 3rd party dll in separate thread but this still blocks main...

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    In my Kivy app I initialise some instrumentation via 3rd party .NET dlls. One of these dlls is badly behaved - takes several seconds to initialise.

    To keep the app GUI responsive, I tried to initialise the instruments in a separate thread but this still results in the GUI becoming unresponsive.

    I know it is this one instrument dll in particular because when I omit that instrument from the configuration the GUI remains responsive.

    Also if I replace initialising each instrument with e.g. time.sleep(3) and print(instr) I see expected behaviour - init_instruments_in_background() executing and GUI remaining responsive throughout.

    Am I attempting something that may be fundamentally not possible? Can it be that this 3rd party dll is just not capable of running in a separate thread without blocking the main thread? I think the reason for the slow execution of this dll is a TCP/IP telnet comms timeout delay. The OS is Windows 10. Python 3.12.4, Kivy 2.3.0

    in main widget in kivy gui


    def init_instruments_in_background(self):

    #instruments are classes defined in Instrument1.py, Instrument2.py, etc
    for instr in ['Instrument1', 'Instrument2', 'Instrument3']:
    globals()[instr] = __import__(instr)
    #THIS OPERATION BLOCKS THE MAIN THREAD IN THE CASE OF ONE INSTRUMENT
    self.Instruments[instr] = globals()[instr].Instrument(<ip_addr>, <port>)

    def init_instruments(self):
    equip_thread = Thread(target=self.init_instruments_in_background, daemon=True)
    equip_thread.start()

    self.init_instruments()

    structure of InstrumentX.py classes


    import clr
    class Instrument:

    def __new__(self, ip_addr, port):

    clr.AddReference('<name_of_.NET_instrument_driver_dll>')
    from <NET_instrument_driver_namespace> import InstumentX
    self.INTERFACE = InstrumentX(ip_addr, port)

    return self.INTERFACE

    Continue reading...

Compartilhe esta Página