1

Python Epoll には、登録済みのファイル記述子を epoll オブジェクトから削除する epoll.unregister という関数があります。これに似たKqueueの機能を知っている人はいますか。kqueue の場合、イベントを削除する方法しか見つかりませんでした。

4

1 に答える 1

2

kqueue.controlイベントの登録または登録解除に使用します。

例:

import select
import os

os.mkfifo('my.fifo')
f = os.open('my.fifo', os.O_RDONLY|os.O_NONBLOCK)

try:
    kq = select.kqueue()

    # Add FD to the queue
    kq.control([select.kevent(f, select.KQ_FILTER_READ, select.KQ_EV_ADD|select.KQ_EV_ENABLE)], 0)

    # Should break as soon as we received something.
    i = 0
    while True:
        events = kq.control(None, 1, 1.0) # max_events, timeout
        print(i, events)
        i += 1
        if len(events) >= 1:
            print('We got:', os.read(f, events[0].data))
            break

    # Remove FD from the queue.
    kq.control([select.kevent(f, select.KQ_FILTER_READ, select.KQ_EV_DELETE)], 0)

    # Should never receive anything now even if we write to the pipe.
    i = 0
    while True:
        events = kq.control(None, 1, 1.0) # max_events, timeout
        print(i, events)
        i += 1
        if len(events) >= 1:
            print('We got:', os.read(f, events[0].data))
            break

finally:
    os.close(f)
    os.remove('my.fifo')

また、kqueueのテスト ケースをチェックして、その使用方法を確認することもできます。(そして のようselect()に、ファイル記述子は、fileno()メソッドを持つ任意の Python オブジェクトにすることもできます。)

于 2013-09-02T16:30:10.353 に答える