Merged in changes to api.py, nginx support

This commit is contained in:
nat
2008-03-18 15:27:19 -04:00
11 changed files with 41 additions and 39 deletions

View File

@@ -68,8 +68,8 @@ def tcp_listener(address):
which accepts connections forever and spawns greenlets for which accepts connections forever and spawns greenlets for
each incoming connection. each incoming connection.
""" """
from eventlet import wrappedfd, util from eventlet import greenio, util
socket = wrappedfd.GreenSocket(util.tcp_socket()) socket = greenio.GreenSocket(util.tcp_socket())
util.socket_bind_and_listen(socket, address) util.socket_bind_and_listen(socket, address)
return socket return socket
@@ -94,8 +94,8 @@ def connect_tcp(address):
""" """
Create a TCP connection to address (host, port) and return the socket. Create a TCP connection to address (host, port) and return the socket.
""" """
from eventlet import wrappedfd, util from eventlet import greenio, util
desc = wrappedfd.GreenSocket(util.tcp_socket()) desc = greenio.GreenSocket(util.tcp_socket())
desc.connect(address) desc.connect(address)
return desc return desc
@@ -187,13 +187,6 @@ def exc_after(seconds, exc):
def get_default_hub(): def get_default_hub():
## TODO some sort of plugin system?
try:
import eventlet.hubs.nginx
return eventlet.hubs.nginx
except ImportError:
pass
try: try:
import eventlet.hubs.libevent import eventlet.hubs.libevent
return eventlet.hubs.libevent return eventlet.hubs.libevent

View File

@@ -23,7 +23,7 @@ THE SOFTWARE.
""" """
from eventlet import tests from eventlet import tests
from eventlet import api, wrappedfd, util from eventlet import api, greenio, util
import socket import socket
@@ -101,7 +101,7 @@ class TestApi(tests.TestCase):
bound_port = server.getsockname()[1] bound_port = server.getsockname()[1]
try: try:
desc = wrappedfd.GreenSocket(util.tcp_socket()) desc = greenio.GreenSocket(util.tcp_socket())
api.trampoline(desc, read=True, write=True, timeout=0.1) api.trampoline(desc, read=True, write=True, timeout=0.1)
except api.TimeoutError: except api.TimeoutError:
pass # test passed pass # test passed
@@ -120,7 +120,7 @@ class TestApi(tests.TestCase):
def go(): def go():
client = util.tcp_socket() client = util.tcp_socket()
desc = wrappedfd.GreenSocket(client) desc = greenio.GreenSocket(client)
desc.connect(('127.0.0.1', bound_port)) desc.connect(('127.0.0.1', bound_port))
try: try:
api.trampoline(desc, read=True, write=True, timeout=0.1) api.trampoline(desc, read=True, write=True, timeout=0.1)

View File

@@ -1,6 +1,5 @@
"""\ """\
@file wrappedfd.py @file greenio.py
@author Bob Ippolito
Copyright (c) 2005-2006, Bob Ippolito Copyright (c) 2005-2006, Bob Ippolito
Copyright (c) 2007, Linden Research, Inc. Copyright (c) 2007, Linden Research, Inc.

View File

@@ -1,5 +1,5 @@
"""\ """\
@file wrappedfd_test.py @file greenio_test.py
Copyright (c) 2006-2007, Linden Research, Inc. Copyright (c) 2006-2007, Linden Research, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -22,12 +22,12 @@ THE SOFTWARE.
""" """
from eventlet import tests from eventlet import tests
from eventlet import api, wrappedfd, util from eventlet import api, greenio, util
import socket import socket
# TODO try and reuse unit tests from within Python itself # TODO try and reuse unit tests from within Python itself
class TestWrappedFd(tests.TestCase): class TestGreenIo(tests.TestCase):
def test_close_with_makefile(self): def test_close_with_makefile(self):
def accept_close_early(listener): def accept_close_early(listener):
# verify that the makefile and the socket are truly independent # verify that the makefile and the socket are truly independent

View File

@@ -125,7 +125,7 @@ class TestHttpd(tests.TestCase):
fd.close() fd.close()
def test_003_passing_non_int_to_read(self): def test_003_passing_non_int_to_read(self):
# This should go in test_wrappedfd # This should go in greenio_test
sock = api.connect_tcp( sock = api.connect_tcp(
('127.0.0.1', 12346)) ('127.0.0.1', 12346))

View File

@@ -203,12 +203,16 @@ class BaseHub(object):
def add_timer(self, timer): def add_timer(self, timer):
scheduled_time = self.clock() + timer.seconds scheduled_time = self.clock() + timer.seconds
self._add_absolute_timer(scheduled_time, timer) self._add_absolute_timer(scheduled_time, timer)
timer.greenlet = current_greenlet
self.track_timer(timer)
return scheduled_time
def track_timer(self, timer):
current_greenlet = greenlet.getcurrent() current_greenlet = greenlet.getcurrent()
if current_greenlet not in self.timers_by_greenlet: if current_greenlet not in self.timers_by_greenlet:
self.timers_by_greenlet[current_greenlet] = {} self.timers_by_greenlet[current_greenlet] = {}
self.timers_by_greenlet[current_greenlet][timer] = True self.timers_by_greenlet[current_greenlet][timer] = True
timer.greenlet = current_greenlet
return scheduled_time
def prepare_timers(self): def prepare_timers(self):
ins = bisect.insort_right ins = bisect.insort_right
@@ -258,5 +262,5 @@ class BaseHub(object):
## actually eventlet's silly way of specifying whether ## actually eventlet's silly way of specifying whether
## a coroutine is "ready to run" or not. ## a coroutine is "ready to run" or not.
timer.cancel() timer.cancel()
print 'Runloop cancelling left-over timer %s' % timer print 'Hub cancelling left-over timer %s' % timer
del self.timers_by_greenlet[greenlet] del self.timers_by_greenlet[greenlet]

View File

@@ -38,14 +38,14 @@ import greenlet
#raise ImportError() #raise ImportError()
try: try:
# use rel if it's available import event
except ImportError:
# use rel if pyevent isn't available
# (rel prints out some annoying notice upon initialization)
import rel import rel
rel.initialize() rel.initialize()
rel.override() rel.override()
except ImportError: import event
pass
import event
class Hub(hub.BaseHub): class Hub(hub.BaseHub):
@@ -101,4 +101,5 @@ class Hub(hub.BaseHub):
def add_timer(self, timer): def add_timer(self, timer):
event.timeout(timer.seconds, timer).add() event.timeout(timer.seconds, timer).add()
self.track_timer(timer)

View File

@@ -28,7 +28,7 @@ import signal
from eventlet import util, pools from eventlet import util, pools
from eventlet import wrappedfd from eventlet import greenio
class DeadProcess(RuntimeError): class DeadProcess(RuntimeError):
pass pass
@@ -55,9 +55,9 @@ class Process(object):
child_stdin = self.popen4.tochild child_stdin = self.popen4.tochild
util.set_nonblocking(child_stdout_stderr) util.set_nonblocking(child_stdout_stderr)
util.set_nonblocking(child_stdin) util.set_nonblocking(child_stdin)
self.child_stdout_stderr = wrappedfd.GreenPipe(child_stdout_stderr) self.child_stdout_stderr = greenio.GreenPipe(child_stdout_stderr)
self.child_stdout_stderr.newlines = '\n' # the default is \r\n, which aren't sent over pipes self.child_stdout_stderr.newlines = '\n' # the default is \r\n, which aren't sent over pipes
self.child_stdin = wrappedfd.GreenPipe(child_stdin) self.child_stdin = greenio.GreenPipe(child_stdin)
self.child_stdin.newlines = '\n' self.child_stdin.newlines = '\n'
self.sendall = self.child_stdin.write self.sendall = self.child_stdin.write

View File

@@ -4,6 +4,8 @@ import sys
from eventlet import api from eventlet import api
from eventlet import httpc from eventlet import httpc
from eventlet.hubs import nginx
def real_application(env, start_response): def real_application(env, start_response):
#result = httpc.get('http://127.0.0.1:8081/') #result = httpc.get('http://127.0.0.1:8081/')
@@ -23,6 +25,9 @@ def wrap_application(master, env, start_response):
def application(env, start_response): def application(env, start_response):
hub = api.get_hub() hub = api.get_hub()
if not isinstance(hub, nginx.Hub):
api.use_hub(nginx)
hub.poll_register = env['ngx.poll_register'] hub.poll_register = env['ngx.poll_register']
hub.poll_unregister = env['ngx.poll_unregister'] hub.poll_unregister = env['ngx.poll_unregister']
hub.sleep = env['ngx.sleep'] hub.sleep = env['ngx.sleep']

View File

@@ -23,12 +23,12 @@ import Queue
from sys import stdout from sys import stdout
from Queue import Empty, Queue from Queue import Empty, Queue
from eventlet import api, coros, httpc, httpd, util, wrappedfd from eventlet import api, coros, httpc, httpd, util, greenio
from eventlet.api import trampoline, get_hub from eventlet.api import trampoline, get_hub
_rpipe, _wpipe = os.pipe() _rpipe, _wpipe = os.pipe()
_rfile = os.fdopen(_rpipe,"r",0) _rfile = os.fdopen(_rpipe,"r",0)
_wrap_rfile = wrappedfd.GreenPipe(_rfile) _wrap_rfile = greenio.GreenPipe(_rfile)
util.set_nonblocking(_rfile) util.set_nonblocking(_rfile)
def _signal_t2e(): def _signal_t2e():

View File

@@ -76,7 +76,7 @@ __original_ssl__ = socket.ssl
def wrap_ssl(sock, certificate=None, private_key=None): def wrap_ssl(sock, certificate=None, private_key=None):
from OpenSSL import SSL from OpenSSL import SSL
from eventlet import wrappedfd, util from eventlet import greenio, util
context = SSL.Context(SSL.SSLv23_METHOD) context = SSL.Context(SSL.SSLv23_METHOD)
print certificate, private_key print certificate, private_key
if certificate is not None: if certificate is not None:
@@ -88,15 +88,15 @@ def wrap_ssl(sock, certificate=None, private_key=None):
## TODO only do this on client sockets? how? ## TODO only do this on client sockets? how?
connection = SSL.Connection(context, sock) connection = SSL.Connection(context, sock)
connection.set_connect_state() connection.set_connect_state()
return wrappedfd.GreenSocket(connection) return greenio.GreenSocket(connection)
def wrap_socket_with_coroutine_socket(): def wrap_socket_with_coroutine_socket():
def new_socket(*args, **kw): def new_socket(*args, **kw):
from eventlet import wrappedfd from eventlet import greenio
s = __original_socket__(*args, **kw) s = __original_socket__(*args, **kw)
set_nonblocking(s) set_nonblocking(s)
return wrappedfd.GreenSocket(s) return greenio.GreenSocket(s)
socket.socket = new_socket socket.socket = new_socket
socket.ssl = wrap_ssl socket.ssl = wrap_ssl