从无尽的 while 循环中在函数之间传递数据

Passing data between functions from endless while loop

一直在努力让它工作,因为我无法在不会结束的 while 循环中使用 return

简而言之,我在一个函数 receive()(无限循环)中的套接字客户端中接收到一条消息,并且需要将该传入消息的结果传递给 main()。尝试使用 yield,因为我不确定还有什么可以实现这一点。我创建了另一个函数来尝试捕获 receive() 函数中的 yield

我知道初始消息到达服务器是因为它输出消息,我知道客户端正在接收服务器的确认消息因为它正在打印。我只是运气不好将该数据传递给 main(),因此其余代码无法正常工作。

我对此很陌生,所以我可能做错了。我应该使用 类 来更轻松地共享数据,但对它们的掌握还不够。希望使用 yield 或其他东西可以解决这个问题。

接收函数:

def receive():
    while True:
        try:
            incoming = client.recv(2048).decode(FORMAT)
            if 'RECEIVED' in incoming:
                confirmation = 'confirmed'
                yield confirmation
            print(incoming)
        except:
            print("Connection interrupted.")
            client.close()
            break

#------------
# also tried
#------------
def receive():
    while True:
        try:
            incoming = client.recv(2048).decode(FORMAT)
            if 'RECEIVED:COMPLETE' in incoming:
                confirmation = 'confirmed'
            else:
                confirmation = 'unconfirmed'
            yield confirmation
        except:
            print("Connection interrupted.")
            client.close()
            break

return函数:

def pass_return(passed_return_value):
    passed_return_value

主要功能(带有各种测试)

def main():
    pass_return(receive())
    # Bunch of code
        if something == True:
            send("some message")
            time.sleep(.25)
            try:
                if confirmation == 'confirmed':
                    # do the business here

#------------
# also tried
#------------
def main():
    # Bunch of code
        if something == True:
            send("some message")
            time.sleep(.25)
            pass_return(receive())
            try:
                if confirmation == 'confirmed':
                    # do the business here

#------------
# also tried
#------------
def main():
    r = pass_return(receive())
    # Bunch of code
        if something == True:
            send("some message")
            time.sleep(.25)
            try:
                if r == 'confirmed':
                    # do the business here

#------------
# even tried
#------------
def main():
    # Bunch of code
        if something == True:
            send("some message")
            time.sleep(.25)
            r = pass_return(receive())
            try:
                if r == 'confirmed':
                    # do the business here

我在 main()receive() 之外声明变量 confirmation(在我的常量所在的文件顶部),否则我会收到 confirmation is undefined 错误.

如果我在 main()print confirmation,它要么不打印任何内容,要么打印 None,所以我的猜测是它只是获取 [= 的初始空值21=] 而不是 yield.

# constants above here
confirmation = str()

# code and such
def pass_return(passed_return_value):
    passed_return_value
def receive():
    #code...
def main():
    #code...

if __name__ == '__main__':
    main()

注意 Python 是单线程的。如果您的程序除了等待数据从外部源到达套接字并对其作出反应外,什么都不做,那么这将起作用:

def receive():
    while True:
        try:
            incoming = client.recv(2048).decode(FORMAT)
            if 'RECEIVED' in incoming:
                yield {'status': 'confirmed', 'message': incoming}
            else:
                yield {'status': 'unconfirmed', 'message': incoming}
        except:
            print("Connection interrupted.")
            client.close()
            break

def main():
    for message in receive():
        print(message)

这里,本程序的大部分时间将花在client.recv(2048)上。 This is a blocking call. 它不会 return 直到收到请求的字节数。在此期间您的程序中不会发生任何其他事情。

一旦接收到字节,就可以yieldmain()里面的for循环可以处理数据。完成后,程序流将从 return 到 client.recv(2048) 并坐在那里。

如果您希望程序在 侦听该套接字上的数据时发生不同的事情,您将需要研究多线程,并将此代码放在后台线程。