Issues with exiting

Supporting 2.7 and 3.2+
jcrobots
Phidgetsian
Posts: 5
Joined: Fri Apr 05, 2019 8:38 pm
Contact:

Issues with exiting

Postby jcrobots » Fri Apr 05, 2019 10:44 pm

I am playing with two 8/8/8 Phidgets, using the Python API on Ubuntu. On MacOS I have no problem, but on Linux this comes up. My script works right the first time, but after I close it or kill it a few times I lose the ability to open channels.

I believe I am closing all of my ports properly, but it seems something may not be exiting properly.

Is it possible to run a "clean" command when my script starts to avoid this issue if it everything wasn't closed properly on the last exit? The only thing that I'm able to do to fix the problem is to reboot Linux.

My code is below, let me know if I am not exiting properly. I'm a beginner with both Python and Phidgets.


Code: Select all



def run():
    try:
       # io is a list which will hold all the channels
       # Only purpose of io is to close them all at the end
        io = []
        LEDs_1 = init_LEDs(DEVICE_2_SERIAL_NUMBER)
        io += LEDs_1
        LEDs_2 = init_LEDs(DEVICE_1_SERIAL_NUMBER)
        io += LEDs_2
        vin_1 = init_vin(DEVICE_1_SERIAL_NUMBER, front=True)
        io += vin_1
        vin_2 = init_vin(DEVICE_2_SERIAL_NUMBER, front=False)
        io += vin_2
        toggle_1 = init_toggle(DEVICE_1_SERIAL_NUMBER)
        io += [toggle_1]
        toggle_2 = init_toggle(DEVICE_2_SERIAL_NUMBER)
        io += [toggle_2]
     
        # Flash LED's based on sensors until keyboard interrupt
        while True:
            for i in LEDS_A:
                LEDs_1[i].setState(bool_a_1)
                LEDs_2[i].setState(bool_a_2)
            for i in LEDS_B:
                LEDs_1[i].setState(bool_b_1)
                LEDs_2[i].setState(bool_b_2)
            time.sleep(1)
        close_ports(io)
        return 0

# A bunch of except blocks are the only way to exit
# All of them are supposed to close all the ports on the way out
# The KeyboardInterrupt functionality doesn't really work
    except PhidgetException as e:
        print("\nExiting with error(s)...")
        DisplayError(e)
        traceback.print_exc(file=open("sensor.log","w"))
        print("Closing sensor channel")
        close_ports(io)
        return 1
    except EndProgramSignal as e:
        print(e)
        print("Closing sensor channel")
        close_ports(io)
        return 1
    except KeyboardInterrupt:
        close_ports(io)
        return 1
    except RuntimeError as e:
        close_ports(io)
        print("Runtime Error: \n\t" + e)
        traceback.print_exc(file=open("sensor.log","w"))
        return 1

# Iterate through a list of channels and close them all
def close_ports(ch_list): 
    for ch in ch_list:
        if ch:  # if the channel isn't "None"
            ch.close()  # close the channel

run()


jcrobots
Phidgetsian
Posts: 5
Joined: Fri Apr 05, 2019 8:38 pm
Contact:

Re: Issues with exiting

Postby jcrobots » Mon Apr 08, 2019 10:07 am

Edit:

Running a script to reset the USB port fixes the issue. Wondering if there is a more natural solution, for example a way to reset the Phidget through the API, or to prevent the ports from failing to close properly.

jdecoux
Labview Developer
Posts: 90
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Issues with exiting

Postby jdecoux » Tue Apr 09, 2019 12:03 pm

You can call:

Code: Select all

Phidget.resetLibrary()


to free up everything in the Phidgets library. This closes all channels, stops all Phidgets-related threads, etc.

Just be sure there are no other Phidgets-related programs you intend to have continue running on your machine, as it will reset the Phidgets library in those as well.

User avatar
Patrick
Lead Developer
Posts: 3098
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Issues with exiting

Postby Patrick » Tue Apr 09, 2019 3:52 pm

The phidgets library should be getting unloaded between python script runs. How are you running the Python script? Just from the command line?

resetLibrary will reset the library, if it is somehow being left loaded by the Python environment between script runs.

Can you enable library logging and post the result during failure case?

-Patrick

Nadav
Phidgetsian
Posts: 11
Joined: Fri Jan 13, 2017 1:50 am
Contact:

Re: Issues with exiting

Postby Nadav » Thu Apr 11, 2019 12:53 pm

jdecoux wrote:You can call:

Code: Select all

Phidget.resetLibrary()


Is there a Phidget 22 equivalent?

jdecoux
Labview Developer
Posts: 90
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Issues with exiting

Postby jdecoux » Thu Apr 11, 2019 2:42 pm

Yes, that function is for Phidget22.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 4 guests