Maybe one of my readers is a bit more experienced when it comes to Python – probably not that hard – and can explain to me why my first script does not work and the second one on the other hand does. Biteule and I only came as far as to suspect, that it has something to do with the threading of the GPIO-callbacks but what exactly the problem is neither of us understands. It’s not like zerorpc is not imported and available in the first script when it gets executed, after all it is able to throw an error (LostRemote: Lost remote after 10s heartbeat), so why does it make a difference where I import the module?
import RPi.GPIO as GPIO import time import zerorpc nodebridge = zerorpc.Client() nodebridge.connect("tcp://127.0.0.1:4242") GPIO.setmode(GPIO.BCM) GPIO.setup(27, GPIO.IN) def switchview(switchview_taster): nodebridge.switchview() try: GPIO.add_event_detect(27, GPIO.FALLING, callback=switchview, bouncetime=600) while 1: time.sleep(100)
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(27, GPIO.IN) def switchview(switchview_taster): import zerorpc nodebridge = zerorpc.Client() nodebridge.connect("tcp://127.0.0.1:4242") nodebridge.switchview() try: GPIO.add_event_detect(27, GPIO.FALLING, callback=switchview, bouncetime=600) while 1: time.sleep(100)
var io = require('socket.io').listen(1234); var zerorpc = require("zerorpc"); var pythonbridge = new zerorpc.Server({ switchview: function(reply) { io.sockets.emit('switchview'); reply(null); } }); pythonbridge.on("error", function(error) { console.error("RPC server error:", error); }); pythonbridge.bind("tcp://0.0.0.0:4242");
UPDATE 12.02.15 – 14:51:
@paviro_ eure Vermutung wird stimmen, kurzes googeln hat das hier gebracht: https://t.co/DBzWoEZI7p => “ZeroRPC itself is not thread safe”
— bytesnake (@bytesnake) 11. Februar 2015
@paviro_ mh zerorpc scheint ein wrapper um zeromq zu sein, da zeromq in C geschrieben wurde ist dieser Teil von zerorpc nicht thread safe — bytesnake (@bytesnake) 11. Februar 2015
@paviro_ das scheint aber ein generelles problem von ZMQ zu sein => “In ØMQ, Contexts are threadsafe objects, but Sockets are not.”
— bytesnake (@bytesnake) 11. Februar 2015