简体   繁体   English

如何在 python 中获取 function 的返回值

[英]How to get a return value of a function in python

import gnsq

class something():
    def __init__(self, pb=None, pk=None, address=None):
        self.pb = pb
        self.pk = pk
        self.address = address

    def connect(self):
        consumer = gnsq.Consumer(self.pb, 'ch', self.address)

        @consumer.on_message.connect
        def response_handler(consumer, msg):
           return msg.body

        consumer.start()

how would i get the return value of response_handler so in turn, I'd be able to pass to the parent function connect() , so when i call it, it will be returning the value of message.body from the child function.我将如何获得response_handler的返回值,所以反过来,我可以传递给父 function connect() ,所以当我调用它时,它将返回来自子 function 的message.body的值。

I would think something like the following:我会想到以下内容:

import gnsq

class something():
    def __init__(self, pb=None, pk=None, address=None):
        self.pb = pb
        self.pk = pk
        self.address = address

    def connect(self):
        consumer = gnsq.Consumer(self.pb, 'ch', self.address)

        @consumer.on_message.connect
        def response_handler(consumer, msg):
           return msg.body

        consumer.start()

       return response_handler

nsq = something('pb', 'pk', 'address')

# should print whatever message.body is
print nsq.connect() 

but It's not working.但它不起作用。 Note: consumer.start() is blocking注意: consumer.start()正在阻塞

What you're asking doesn't make sense in the context of what the Consumer() really is.你所问的在Consumer()真正是什么的背景下没有意义。

In your connect() method, you set up a consumer, set up a response handler and start the consumer with consumer.start() .在您的connect()方法中,您设置了一个消费者,设置了一个响应处理程序并使用consumer.start()启动消费者。 From that point onward, whenever there is a message to consume, the consumer will call the handler with that message.从那时起,只要有消息要消费,消费者就会使用该消息调用处理程序。 Not just once, but again and again.不只是一次,而是一次又一次。

Your handler may be called many times and unless the consumer is closed, you never know when it will be done - so, there's no way your connect() method could return the complete result.您的处理程序可能会被多次调用,除非消费者已关闭,否则您永远不知道何时完成 - 因此,您的connect()方法无法返回完整的结果。

What you could do is have the connect method return a reference to a collection that will at any time contain all the messages collected so far.您可以做的是让 connect 方法返回对集合的引用,该集合将在任何时候包含迄今为止收集的所有消息。 It would be empty at first, but after some time, could contain all the received messages.起初它是空的,但一段时间后,它可能包含所有收到的消息。

Something like:就像是:

import gnsq

class Collector():
    def __init__(self, topic, address):
        self.topic = topic
        self.address = address
        self.messages = []

    def connect(self):
        self.messages = []
        consumer = gnsq.Consumer(self.pb, 'ch', self.address)

        @consumer.on_message.connect
        def response_handler(consumer, msg):
           self.messages.append(msg)

        consumer.start()
        return self.messages

I don't think this is really how you want to be using this, it would only really make sense if you provide more context on why and how you want to use this output.我不认为这真的是你想要使用它的方式,如果你提供更多关于你为什么以及如何使用这个 output 的上下文,它才会真正有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM