import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("iot.eclipse.org", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
using PyCall
@pyimport paho.mqtt.client as mqtt
function on_connect(client, userdata, flags, rc)
println("Connected with result code "*string(rc))
client[:subscribe]("$SYS/#")
end
function on_message(client, userdata, msg)
println(msg[:topic]*" "*string(msg[:payload]))
end
client = mqtt.Client()
client[:on_connect] = on_connect
client[:on_message] = on_message
client[:connect]("iot.eclipse.org", 1883, 60)
client[:loop_forever]()
ERROR: LoadError: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'PyCall.jl_Function' object has no attribute 'func_code'",)
File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1261, in loop_forever
rc = self.loop(timeout, max_packets)
File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 811, in loop
rc = self.loop_read(max_packets)
File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1073, in loop_read
rc = self._packet_read()
File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1475, in _packet_read
rc = self._packet_handle()
File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1949, in _packet_handle
return self._handle_connack()
File "/home/kjwiik/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 2001, in _handle_connack
argcount = self.on_connect.func_code.co_argcount
[inlined code] from /home/kjwiik/.julia/v0.4/PyCall/src/exception.jl:81
in pycall at /home/kjwiik/.julia/v0.4/PyCall/src/PyCall.jl:356
in call at /home/kjwiik/.julia/v0.4/PyCall/src/PyCall.jl:372
in include at ./boot.jl:261
in include_from_node1 at ./loading.jl:320
while loading /home/kjwiik/proj/telescopes/software/mqttclient.jl, in expression starting on line 19
ERROR: LoadError: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'PyCall.jl_Function' object has no attribute 'func_code'",)
ERROR: LoadError: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'PyCall.jl_Function' object has no attribute 'func_code'",)The foo.func_code attribute of a Python function foo returns a code object representing the compiled Python bytecode for foo. A Julia function is not going to have this attribute, because it has no Python bytecode.
if sys.version_info[0] < 3:
argcount = self.on_connect.func_code.co_argcount
else:
argcount = self.on_connect.__code__.co_argcount
ERROR: LoadError: PyError (:PyObject_Call) <class 'AttributeError'>
AttributeError("'PyCall.jl_Function' object has no attribute '__code__'",)
OK, I see, thanks. I found this from the module source (client.py):if sys.version_info[0] < 3:
argcount = self.on_connect.func_code.co_argcount
else:
argcount = self.on_connect.__code__.co_argcount
Maybe this will work?
client.on_connect = PyCall.jlfun2pyfun(on_connect)
client.on_message = PyCall.jlfun2pyfun(on_message)
Maybe this will work?client.on_connect = PyCall.jlfun2pyfun(on_connect)
client.on_message = PyCall.jlfun2pyfun(on_message)
jlfun2pyfun simply inserts an extra level of indirection through a native Python lambda, which does have a func_code.