mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-01 01:09:22 +01:00
f7ab2564fe
Closes #588.
45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
"""
|
|
Socket handling driver using the selectors module. epoll, kqueue, and devpoll
|
|
are used internally when available.
|
|
"""
|
|
|
|
import selectors
|
|
import threading
|
|
|
|
from pylinkirc import world
|
|
from pylinkirc.log import log
|
|
|
|
SELECT_TIMEOUT = 0.5
|
|
|
|
selector = selectors.DefaultSelector()
|
|
|
|
def _process_conns():
|
|
"""Main loop which processes connected sockets."""
|
|
|
|
while not world.shutting_down.is_set():
|
|
for socketkey, mask in selector.select(timeout=SELECT_TIMEOUT):
|
|
irc = socketkey.data
|
|
if mask & selectors.EVENT_READ:
|
|
irc._run_irc()
|
|
|
|
def register(irc):
|
|
"""
|
|
Registers a network to the global selectors instance.
|
|
"""
|
|
log.debug('selectdriver: registering %s for network %s', irc._socket, irc.name)
|
|
selector.register(irc._socket, selectors.EVENT_READ, data=irc)
|
|
|
|
def unregister(irc):
|
|
"""
|
|
Removes a network from the global selectors instance.
|
|
"""
|
|
log.debug('selectdriver: de-registering %s for network %s', irc._socket, irc.name)
|
|
selector.unregister(irc._socket)
|
|
|
|
def start():
|
|
"""
|
|
Starts a thread to process connections.
|
|
"""
|
|
t = threading.Thread(target=_process_conns, name="Selector driver loop")
|
|
t.start()
|