通过无限 while 循环在函数之间传递数据

liq*_*ock 0 python variables if-statement function while-loop

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

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

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

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

接收函数:

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

Run Code Online (Sandbox Code Playgroud)

返回函数:

def pass_return(passed_return_value):
    passed_return_value
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

如果我print confirmation输入main(),它要么不打印任何内容,或者None,所以我的猜测是它只是获取 的初始空值confirmation而不是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()
Run Code Online (Sandbox Code Playgroud)

Tom*_*lak 5

请注意,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)
Run Code Online (Sandbox Code Playgroud)

在这里,这个程序的大部分时间将花费在client.recv(2048). 这是一个阻塞调用。在收到请求的字节数之前它不会返回。在此期间,您的程序中不会发生任何其他事情。

一旦收到字节,您就可以yield,并且for内部循环main()可以处理数据。完成后,程序流程将返回client.recv(2048)并停留在那里。

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