Home  >  Q&A  >  body text

How to get variable value in blocking function in python

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()
迷茫迷茫2653 days ago857

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-06-17 09:17:52

    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

    reply
    0
  • Cancelreply