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

조회 수: 12 (최근 30일)
Ran Shaham
Ran Shaham 2022년 11월 13일
답변: Abhijeet 2023년 8월 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