Defining callback function for methods called from python (pika library)

16 次查看(过去 30 天)
Hello,
I am trying to use the "py.*" functionality in matlab.
I am calling the pika library to connect and consume from a RabbitMQ Server.
While the following code does work:
channel = connection.channel();
channel.queue_declare(queue='hello');
channel.basic_publish(exchange='exchangename', routing_key='key.name', body='somemessage',properties=py.pika.BasicProperties(reply_to='hello'))
connection.close();
The other part does not work:
connection_r = py.pika.BlockingConnection(py.pika.ConnectionParameters(host='localhost'));
channel_r = connection_r.channel();
channel_r.queue_declare(queue='hello');
channel_r.basic_consume(queue='hello', auto_ack=true,on_message_callback= @Callbackfunction);
channel_r.start_consuming()
I get erros saying that i can't send MATLAB functions in to the python env. And i also can't just define the functions there because it does not work.
Is there anyway to make this work ? to connect Pika's basic_consume method to a matlab callback ?
Thanks.

回答(1 个)

Abhijeet
Abhijeet 2023-8-30
Hi Ran,
I can understand that you want to use a callback function written in MATLAB and use it inside ‘basic_consume’ function defined in pika library. In order to use MATLAB callback function inside ‘basic_consume’ you can refer the following steps: -
  1. You can write a MATLAB script which defines the callback function.
  2. Inside a python script write a callback function that calls MATLAB using MATLAB Engine API for Python’. Please refer to the following example code :-
import matlab.engine
def python_callback( ):
eng = matlab.engine.start_matlab()
eng.Callbackfunction(nargout=0)
eng.quit()
3. Use the callback written inside python script as argument for ‘basic_consume’ function.
To learn more about the ‘MATLAB Engine API for Python’ please refer to the following documentation: https://www.mathworks.com/help/matlab/matlab_external/get-started-with-matlab-engine-for-python.html

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by