How to get variable value in blocking function in python
迷茫
迷茫 2017-06-17 09:15:52
0
1
952

Currently, C embedded python function is used
The python function is a blocking type of receiving server messages
There will be no return value until the monitoring is exited
A callback function of python will be called when receiving a message during monitoring , you can get the received message
So how to return the message to C

EDIT:
The code is as follows callback printing can print correct data. The problem is
The rabbitMQ client written using the pika library here is to avoid using the C library of
rabbitMQ
Now there is a consideration that is to Write a python-adjustable module
in C and then adjust it in the callback function, but it feels a bit ugly.

#!/usr/bin/env python
import pika
import sys

message = ""

def callback(ch, method, properties, body):
    message = body
    print(method.routing_key)
    return message    

def consume():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue

    channel.queue_bind(exchange='normalEx',
                       routing_key='remote',
                       queue=queue_name)

    print(' [*] Waiting for logs. To exit press CTRL+C')

    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)

    channel.start_consuming()
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
小葫芦

Congested functions need to wait until data is received or wait for timeout before returning. If you want to return quickly, use a non-blocking method, but this will be more painful

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template