Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an IOLoop implementation based on asyncio and make it the default if possible #310

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 70 additions & 1 deletion pyftpdlib/ioloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def handle_accepted(self, sock, addr):
import threading
except ImportError:
import dummy_threading as threading
try:
import asyncio
except ImportError:
_has_asyncio = False
else:
_has_asyncio = True

from ._compat import callable
from .log import config_logging
Expand Down Expand Up @@ -735,11 +741,74 @@ def poll(self,
inst.handle_close()


# ===================================================================
# --- asyncio - Python >= 3.3
# ===================================================================

if _has_asyncio:

class Asyncio(_IOLoop):
"""asyncio based poller."""

def __init__(self, loop=None):
_IOLoop.__init__(self)
self._asyncio_loop = loop if loop else asyncio.new_event_loop()

@classmethod
def instance(cls):
"""Return a global IOLoop instance."""
if cls._instance is None:
cls._lock.acquire()
try:
if cls._instance is None:
cls._instance = cls(asyncio.get_event_loop())
finally:
cls._lock.release()
return cls._instance

def register(self, fd, inst, events):
if events & self.READ:
self._asyncio_loop.add_reader(fd, self._read_ready, inst)
if events & self.WRITE:
self._asyncio_loop.add_writer(fd, self._write_ready, inst)
self.socket_map[fd] = inst

def unregister(self, fd):
try:
del self.socket_map[fd]
except KeyError:
pass
else:
self._asyncio_loop.remove_reader(fd)
self._asyncio_loop.remove_writer(fd)

def modify(self, fd, events):
inst = self.socket_map.get(fd)
if inst is not None:
self.unregister(fd)
self.register(fd, inst, events)

def poll(self, timeout):
if timeout:
self._asyncio_loop.call_later(timeout, self._asyncio_loop.stop)
self._asyncio_loop.run_forever()

def _read_ready(self, inst):
if inst.readable():
_read(inst)

def _write_ready(self, inst):
if inst.writable():
_write(inst)


# ===================================================================
# --- choose the better poller for this platform
# ===================================================================

if hasattr(select, 'epoll'): # epoll() - Linux
if _has_asyncio: # asyncio - Python >= 3.3
IOLoop = Asyncio
elif hasattr(select, 'epoll'): # epoll() - Linux
IOLoop = Epoll
elif hasattr(select, 'kqueue'): # kqueue() - BSD / OSX
IOLoop = Kqueue
Expand Down