簡體   English   中英

從無限的while循環中在函數之間傳遞數據

[英]Passing data between functions from endless while loop

一直在努力讓它工作,因為我不能在一個不會結束的 while 循環中使用return

簡而言之,我在一個 function receive() (無限循環)的套接字客戶端中接收到一條消息,並且需要將該傳入消息的結果傳遞給main() 嘗試使用yield ,因為我不確定還有什么可以實現這一點。 我創建了另一個 function 來嘗試在receive() function 中捕獲yield

我知道初始消息到達服務器是因為它輸出消息,並且我知道客戶端正在接收服務器的確認消息,因為它正在打印它。 我只是沒有運氣將該數據傳遞給main() ,因此代碼的 rest 將無法正常工作。

我對此很陌生,所以我可能做錯了。 我應該使用類來更輕松地共享數據,但還沒有充分掌握它們。 希望使用產量或其他東西可以解決這個問題。

接收 function:

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

返回 function:

def pass_return(passed_return_value):
    passed_return_value

主function(帶各種測試)

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 ,它要么不打印,要么不打印,所以我的猜測是它只是獲得了confirmation的初始None值而不是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)上。 這是一個阻塞調用。 在收到請求的字節數之前它不會返回。 在此期間,您的程序中不會發生任何其他事情。

一旦接收到字節,您就可以yield ,並且main()中的for循環可以處理數據。 完成后,程序流將返回到client.recv(2048)並坐在那里。

如果您希望程序偵聽該套接字上的數據的同時發生不同的事情,則需要研究多線程,並將此代碼放在后台線程上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM