簡體   English   中英

Python:選擇或輪詢threading.event

[英]Python: select or poll on threading.event

我最近在讀,BSD kqueue可以接受各種事件,而不僅僅是文件描述符。 但是對於Linux用戶,它看起來像這樣:(來自socketserver python stdlib)

#self being passed to select is a listening socket
try:
    while not self.__shutdown_request:
        # XXX: Consider using another file descriptor or
        # connecting to the socket to wake this up instead of
        # polling. Polling reduces our responsiveness to a
        # shutdown request and wastes cpu at all other times.
        r, w, e = _eintr_retry(select.select, [self], [], [],
                               poll_interval)
        if self in r:
            self._handle_request_noblock()

        self.service_actions()

是否有一些聰明的方法可以通過selectpoll檢查threading.Event() ,還是不可避免的情況,即必須連接另一個套接字來監聽關閉事件?

編輯:我想要的是這樣的: select.select([self, clever_wrapper(self.__shutdown_request)], [], [])

是否有一些聰明的方法可以通過select或poll檢查threading.Event(),還是不可避免的情況,即必須連接另一個套接字來監聽關閉事件?

我不確定我是否理解正確,但是如果您想檢測對等設備的關閉,則可以簡單地通過select和poll以及隨后的read操作來完成。 也就是說,如果select / poll返回該套接字已被讀取以讀取,然后又不讀取並返回任何數據但也沒有錯誤,那么您就知道對等方發出了關閉請求。

不需要第二個插槽,當然第二個連接甚至無法檢測到第一個連接是否已關閉。

暫無
暫無

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

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