簡體   English   中英

Python線程和輸入

[英]Python Threading and Input

我是Python threading新手,我需要一些代碼的幫助,我正在將IRC客戶端作為一種業余愛好,但我需要用戶能夠輸入文本以及從IRC服務器提取消息的能力,等等​​。 。

在此示例中,在獲取用戶輸入的同時, . 在用戶輸入文本之后輸出。我希望輸入與在監聽功能中輸出的內容分開。

import threading
import time
import sys


def listen():
    sys.stdout.write("Listening...")
    sys.stdout.flush()
    for i in range(5):
        time.sleep(1)
        sys.stdout.write(".")
        sys.stdout.flush()
    return

def test_input():
    input("Here: ")
    return

threads = []
listen_thread = threading.Thread(target=listen)
threads.append(listen_thread)
listen_thread.start()

input_thread = threading.Thread(target=test_input)
threads.append(input_thread)
input_thread.start()

目前,如果我輸入“ hello,你好嗎”,我會得到: Listening...Here: .hell.o there. how ar.e you. Listening...Here: .hell.o there. how ar.e you.

Listening........ Here: hello there how are you . 在“聽”之后

(對不起,措辭)

在啟動listen()線程之后,可以立即啟動test_input()線程。 並且因為test_input()線程並行運行,所以您立即打印“ Here”。
僅在打印所有點后才應調用a.start()。 (需要5秒鍾)。

也嘗試使用更具描述性的變量名。

暫無
暫無

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

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