{"action":"created","issue":{"url":"https://api.github.com/repos/piface/pifacedigitalio/issues/27","labels_url":"https://api.github.com/repos/piface/pifacedigitalio/issues/27/labels{/name}","comments_url":"https://api.github.com/repos/piface/pifacedigitalio/issues/27/comments","events_url":"https://api.github.com/repos/piface/pifacedigitalio/issues/27/events","html_url":"https://github.com/piface/pifacedigitalio/issues/27","id":66917724,"number":27,"title":"Problem when deactivating the SwitchEventListener","user":{"login":"StefJar","id":8638279,"avatar_url":"https://avatars.githubusercontent.com/u/8638279?v=3","gravatar_id":"","url":"https://api.github.com/users/StefJar","html_url":"https://github.com/StefJar","followers_url":"https://api.github.com/users/StefJar/followers","following_url":"https://api.github.com/users/StefJar/following{/other_user}","gists_url":"https://api.github.com/users/StefJar/gists{/gist_id}","starred_url":"https://api.github.com/users/StefJar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StefJar/subscriptions","organizations_url":"https://api.github.com/users/StefJar/orgs","repos_url":"https://api.github.com/users/StefJar/repos","events_url":"https://api.github.com/users/StefJar/events{/privacy}","received_events_url":"https://api.github.com/users/StefJar/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-04-07T15:34:57Z","updated_at":"2015-04-07T16:17:45Z","closed_at":"2015-04-07T16:17:45Z","body":"Hej\r\n\r\nI am trying to exit my Python program when pressing a Button from the PiFace. A Event Listener is used, like described at the examples. It seems that \"listener.deactivate()\" makes a join on active thread. If it is the main thread of the process it fails and lead to a python hang up.\r\nI attach a demo code:\r\n\r\n```python\r\n#!/usr/bin/python\r\n# -*- coding: utf-8 -*-\r\n'''\r\nCreated on 03.04.2015\r\n\r\n@author: stj\r\n'''\r\n\r\nimport os\r\nimport sys\r\nimport distutils.spawn\r\nimport pifacecad\r\nimport time\r\n\r\ndef checkExecutableProgram(program):\r\n    print('check '+program)\r\n    def is_exe(fpath):\r\n        return os.path.isfile(fpath) and os.access(fpath, os.X_OK) \r\n\r\n    fpath, fname = os.path.split(program)\r\n    if fpath:\r\n        if is_exe(program):\r\n            return program\r\n    else:\r\n        for path in os.environ[\"PATH\"].split(os.pathsep):\r\n            path = path.strip('\"')\r\n            exe_file = os.path.join(path, program)\r\n            if is_exe(exe_file):\r\n                print(' is executable')\r\n                return exe_file\r\n\r\n    return None\r\n\r\ndef getExecutableFileList (searchPath, extra=['py']):\r\n    print('crawl '+searchPath)\r\n    pL = []\r\n    for (dirpath, dirnames, filenames) in os.walk(searchPath):\r\n        for f in filenames:\r\n#            if (f.endswith('py') is True) or (checkExecutableProgram(f) is not None):\r\n#if (f.endswith('py') is True):\r\n            if (checkExecutableProgram(f) is not None):            \r\n                pL.append(os.path.join(dirpath,f))\r\n    return pL\r\n\r\n\r\ndef printToLCD(line1, line2):\r\n    global cad\r\n    \r\n    prtStr = line1 + '\\n' + line2\r\n    cad.lcd.clear()\r\n    cad.lcd.write(prtStr)\r\n    \r\ndef updateDisplayFiles():\r\n    global gIndx\r\n    global gFL\r\n    global gFLlen\r\n    \r\n    if gIndx < 0:\r\n        printToLCD('<none>','<none>')\r\n        return\r\n    l1 = '> ' + gFL[gIndx]\r\n    l2 = '<none>' if (gIndx+1 >= gFLlen) else gFL[gIndx+1] \r\n    printToLCD(l1,l2)\r\n\r\ndef moveUp(e):\r\n    global gIndx\r\n    \r\n    if gIndx > 0:\r\n        gIndx -= 1\r\n    updateDisplayFiles()\r\n\r\ndef moveDown (e):\r\n    global gIndx\r\n    \r\n    if gIndx < gFLlen-1:\r\n        gIndx += 1\r\n    else:\r\n        gIndx = gFLlen - 1\r\n    updateDisplayFiles()\r\n\r\ndef execute(e):\r\n    global gIndx\r\n    \r\n    if gIndx < 0:\r\n        printToLCD('nothing to execute','')\r\n    else:\r\n        printToLCD('execute','wait ...')\r\n    os.system( gFL[gIndx])\r\n    #execfile(gFL[gIndx] , {} )\r\n    printToLCD('finished','')\r\n\r\ndef printEvent(e):\r\n    print(e)\r\n\r\n\r\ndef exitCB (e):\r\n    global listener\r\n    \r\n    listener.deactivate()\r\n    sys.exit(0)\r\n\r\n\r\nif __name__ == '__main__':\r\n    global cad\r\n    global listener\r\n    global actionHandler\r\n\r\n    cad = None\r\n    listener = None\r\n    gFL = getExecutableFileList(os.path.expanduser(\"~\"))\r\n    gFLlen = len(gFL)\r\n    gIndx = -1 if gFLlen < 1 else 0\r\n\r\n    actionHandler ={\r\n        6 : moveUp,\r\n        7 : moveDown,\r\n        5 : execute,\r\n        4 : exitCB\r\n        }\r\n     \r\n    cad = pifacecad.PiFaceCAD()\r\n    cad.lcd.backlight_on()\r\n\r\n\r\n    printToLCD('starter v.1.0', 'select file')\r\n    time.sleep(2)\r\n    updateDisplayFiles()\r\n    try:\r\n        listener = pifacecad.SwitchEventListener(chip=cad)\r\n        for i in range(8):\r\n            h = actionHandler.get(i, printEvent)\r\n            listener.register(i, pifacecad.IODIR_FALLING_EDGE, h)\r\n        listener.activate()\r\n    except Exception as e:\r\n        print(e)\r\n        listener.deactivate()\r\n    \r\n    sys.exit(0)\r\n```\r\n\r\nand the consol error print:\r\n```\r\nException in thread Thread-1:\r\nTraceback (most recent call last):\r\n  File \"/usr/lib/python3.2/threading.py\", line 740, in _bootstrap_inner\r\n    self.run()\r\n  File \"/usr/lib/python3.2/threading.py\", line 693, in run\r\n    self._target(*self._args, **self._kwargs)\r\n  File \"/usr/lib/python3/dist-packages/pifacecommon/interrupts.py\", line 339, in handle_events\r\n    function(event)\r\n  File \"stjStarter.py\", line 99, in exitCB\r\n    listener.deactivate()\r\n  File \"/usr/lib/python3/dist-packages/pifacecommon/interrupts.py\", line 220, in deactivate\r\n    self.dispatcher.join()\r\n  File \"/usr/lib/python3.2/threading.py\", line 844, in join\r\n    raise RuntimeError(\"cannot join current thread\")\r\nRuntimeError: cannot join current thread\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/piface/pifacedigitalio/issues/comments/90628582","html_url":"https://github.com/piface/pifacedigitalio/issues/27#issuecomment-90628582","issue_url":"https://api.github.com/repos/piface/pifacedigitalio/issues/27","id":90628582,"user":{"login":"StefJar","id":8638279,"avatar_url":"https://avatars.githubusercontent.com/u/8638279?v=3","gravatar_id":"","url":"https://api.github.com/users/StefJar","html_url":"https://github.com/StefJar","followers_url":"https://api.github.com/users/StefJar/followers","following_url":"https://api.github.com/users/StefJar/following{/other_user}","gists_url":"https://api.github.com/users/StefJar/gists{/gist_id}","starred_url":"https://api.github.com/users/StefJar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/StefJar/subscriptions","organizations_url":"https://api.github.com/users/StefJar/orgs","repos_url":"https://api.github.com/users/StefJar/repos","events_url":"https://api.github.com/users/StefJar/events{/privacy}","received_events_url":"https://api.github.com/users/StefJar/received_events","type":"User","site_admin":false},"created_at":"2015-04-07T16:17:45Z","updated_at":"2015-04-07T16:17:45Z","body":"The barrier solved the problem. Maybe you can update the example section at the documentation [GitHub](http://piface.github.io/pifacecad/example.html) by adding the barrier information. Thanks for the fast reply. "}}