簡體   English   中英

超時后停止阻塞 python 線程

[英]Stop a blocking python thread after timeout

我遇到的問題很難找到已經制定的解決方案。

我正在運行一個新的 python 線程,該線程隨后被套接字連接 (python-socketio) 阻塞,因為它無限期地等待數據。 但我需要在 5 分鍾后關閉此線程。 我試圖設置一個計時器,用sys.exit()關閉線程,但我發現這是在關閉計時器線程本身。

這是到目前為止的代碼:

class LiveMatches(commands.Cog):
    def __init__(self, client):
        self.client = client 

    def connect_to_scorebot(self, id):
        feed = MatchFeed(self.client)
        feed.main(id) # when this function is called, 
# it will block the newly created thread to receive the data with the socket

    def create_thread(self, id):
        # we create the thread and call launch_match that will connect to
        # the scorebot
        new_thread = threading.Thread(target=self.connect_to_scorebot, args=(id,))
        # start the thread
        new_thread.start()

幾個選項:

  1. 您可以在線程中使用的套接字上設置超時,以便它在一段時間后從阻塞中返回。
  2. 使用帶有超時的select.select()來輪詢套接字以獲取數據,定期檢查線程是否應該退出。

#1 的示例:

import threading
import socket
import select

# A server that echos only the first data received from a client
def server():
    s = socket.socket()
    s.bind(('',5000))
    s.listen()
    print('server: running')
    while True:
        c,a = s.accept()
        print('server: client connected')
        with c: # closes client socket when with block exits
            echoed = False
            while True:
                data = c.recv(1024)
                if not data: break
                if not echoed:
                    print('server: responding',data)
                    c.sendall(data)
                    echoed = True
        print('server: client disconnected')

def client():
    s = socket.socket()
    s.connect(('localhost',5000))
    with s: # closes client socket when with block exits
        try:
            s.settimeout(5) # 5-second timeout if no data received.
            print('client: send one')
            s.sendall(b'one')
            print('client: got',s.recv(1024))
            print('client: send two')
            s.sendall(b'two')
            print('client: got',s.recv(1024))  # this will timeout
        except socket.timeout:
            print('client: timed out')

# Start server thread.
# As a daemon, it will exit if main thread and client thread both exit.
threading.Thread(target=server,daemon=True).start()

t = threading.Thread(target=client)
t.start()
t.join() # wait for client thread to exit.
t = threading.Thread(target=client)
t.start()
t.join() # wait for client thread to exit.

Output:

server: running
client: send one
server: client connected
server: responding b'one'
client: got b'one'
client: send two
client: timed out
server: client disconnected
client: send one
server: client connected
server: responding b'one'
client: got b'one'
client: send two
client: timed out

注意服務器沒有打印第二個客戶端斷開連接。 由於是daemon線程,當主線程和client線程都退出時就終止了,超時沒來得及識別client斷開連接。 如果您想要更干凈的退出行為,請不要使用守護線程。

暫無
暫無

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

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