Renamed coros.event to coros.Event to be compatible with PEP-8 some more.

This commit is contained in:
Ryan Williams
2009-12-16 00:01:12 -08:00
parent ed958aed54
commit ba34bb7a65
15 changed files with 79 additions and 73 deletions

1
NEWS
View File

@@ -1,6 +1,7 @@
0.9.3
=====
* Renamed coros.event to coros.Event
* Removed previously-deprecated features tcp_server, GreenSSL, erpc, and trap_errors.
* Removed saranwrap as an option for making db connections nonblocking in db_pool.

View File

@@ -17,7 +17,7 @@ Eventlet features:
* utilities for spawning and controlling greenlet execution:
api.spawn, api.kill, proc module
* utilities for communicating between greenlets:
coros.event, coros.queue, proc module
coros.Event, coros.Queue, proc module
* standard Python modules that won't block the reactor:
eventlet.green package
* utilities specific to twisted hub:
@@ -61,7 +61,7 @@ to call from anywhere:
1. Greenlet creation functions: api.spawn, proc.spawn,
twistedutil.deferToGreenThread and others based on api.spawn.
2. send(), send_exception(), poll(), ready() methods of coros.event
2. send(), send_exception(), poll(), ready() methods of coros.Event
and coros.Queue.
3. wait(timeout=0) is identical to poll(). Currently only Proc.wait

View File

@@ -1,6 +1,7 @@
import collections
import time
import traceback
import warnings
from eventlet import api
@@ -15,7 +16,7 @@ class NOT_USED:
NOT_USED = NOT_USED()
class event(object):
class Event(object):
"""An abstraction where an arbitrary number of coroutines
can wait for one event from another.
@@ -28,7 +29,7 @@ class event(object):
They are ideal for communicating return values between coroutines.
>>> from eventlet import coros, api
>>> evt = coros.event()
>>> evt = coros.Event()
>>> def baz(b):
... evt.send(b + 1)
...
@@ -50,7 +51,7 @@ class event(object):
Can only be called after :meth:`send` has been called.
>>> from eventlet import coros
>>> evt = coros.event()
>>> evt = coros.Event()
>>> evt.send(1)
>>> evt.reset()
>>> evt.send(2)
@@ -111,7 +112,7 @@ class event(object):
:meth:`send`.
>>> from eventlet import coros, api
>>> evt = coros.event()
>>> evt = coros.Event()
>>> def wait_on():
... retval = evt.wait()
... print "waited for", retval
@@ -141,7 +142,7 @@ class event(object):
result and then returns immediately to the parent.
>>> from eventlet import coros, api
>>> evt = coros.event()
>>> evt = coros.Event()
>>> def waiter():
... print 'about to wait'
... result = evt.wait()
@@ -184,6 +185,10 @@ class event(object):
# the arguments and the same as for greenlet.throw
return self.send(None, args)
def event(*a, **kw):
warnings.warn("The event class has been capitalized! Please construct"
" Event objects instead.", DeprecationWarning, stacklevel=2)
return Event(*a, **kw)
class Semaphore(object):
"""An unbounded semaphore.
@@ -342,7 +347,7 @@ class metaphore(object):
"""
def __init__(self):
self.counter = 0
self.event = event()
self.event = Event()
# send() right away, else we'd wait on the default 0 count!
self.event.send()
@@ -391,7 +396,7 @@ def execute(func, *args, **kw):
>>> evt.wait()
('foo', 1)
"""
evt = event()
evt = Event()
def _really_execute():
evt.send(func(*args, **kw))
api.spawn(_really_execute)
@@ -583,7 +588,7 @@ class Actor(object):
serially.
"""
self._mailbox = collections.deque()
self._event = event()
self._event = Event()
self._killer = api.spawn(self.run_forever)
self._pool = CoroutinePool(min_size=0, max_size=concurrency)
@@ -592,7 +597,7 @@ class Actor(object):
while True:
if not self._mailbox:
self._event.wait()
self._event = event()
self._event = Event()
else:
# leave the message in the mailbox until after it's
# been processed so the event doesn't get triggered
@@ -629,11 +634,11 @@ class Actor(object):
...
>>> a = Greeter()
This example uses events to synchronize between the actor and the main
This example uses Events to synchronize between the actor and the main
coroutine in a predictable manner, but this kinda defeats the point of
the :class:`Actor`, so don't do it in a real application.
>>> evt = event()
>>> evt = Event()
>>> a.cast( ("message 1", evt) )
>>> evt.wait() # force it to run at this exact moment
received message 1

View File

@@ -184,7 +184,7 @@ class Pool(object):
>>> from eventlet import coros
>>> import string
>>> pool = coros.CoroutinePool(max_size=5)
>>> pausers = [coros.event() for x in xrange(2)]
>>> pausers = [coros.Event() for x in xrange(2)]
>>> def longtask(evt, desc):
... print "%s woke up with %s" % (desc, evt.wait())
...

View File

@@ -15,13 +15,13 @@ you can "link":
* ``p.link(obj)`` - notify *obj* when the coroutine is finished
What "notify" means here depends on the type of *obj*: a callable is simply
called, an :class:`~eventlet.coros.event` or a :class:`~eventlet.coros.queue`
called, an :class:`~eventlet.coros.Event` or a :class:`~eventlet.coros.queue`
is notified using ``send``/``send_exception`` methods and if *obj* is another
greenlet it's killed with :class:`LinkedExited` exception.
Here's an example:
>>> event = coros.event()
>>> event = coros.Event()
>>> _ = p.link(event)
>>> event.wait()
3
@@ -237,7 +237,7 @@ class Source(object):
link. It is possible to link to events, queues, greenlets and callables.
>>> source = Source()
>>> event = coros.event()
>>> event = coros.Event()
>>> _ = source.link(event)
Once source's :meth:`send` or :meth:`send_exception` method is called, all

View File

@@ -49,7 +49,7 @@ def tpool_trampoline():
def esend(meth,*args, **kwargs):
global _reqq, _rspq
e = coros.event()
e = coros.Event()
_reqq.put((e,meth,args,kwargs))
return e

View File

@@ -8,7 +8,7 @@ from twisted.python import failure
from eventlet import proc
from eventlet.api import getcurrent
from eventlet.coros import Queue, event
from eventlet.coros import Queue, Event
class ValueQueue(Queue):
@@ -36,17 +36,17 @@ class ValueQueue(Queue):
return self.items and self.items[-1][1] is not None
class Event(event):
class Event(Event):
def send(self, value, exc=None):
if self.ready():
self.reset()
return event.send(self, value, exc)
return Event.send(self, value, exc)
def send_exception(self, *throw_args):
if self.ready():
self.reset()
return event.send_exception(self, *throw_args)
return Event.send_exception(self, *throw_args)
class Producer2Event(object):

View File

@@ -181,7 +181,7 @@ class TestApi(TestCase):
evt.send('sent via event')
from eventlet import coros
evt = coros.event()
evt = coros.Event()
api.spawn(sender, evt)
api.sleep(0) # lets the socket enter accept mode, which
# is necessary for connect to succeed on windows

View File

@@ -4,7 +4,7 @@ from eventlet import coros, api
class TestEvent(SilencedTestCase):
def test_waiting_for_event(self):
evt = coros.event()
evt = coros.Event()
value = 'some stuff'
def send_to_event():
evt.send(value)
@@ -12,7 +12,7 @@ class TestEvent(SilencedTestCase):
self.assertEqual(evt.wait(), value)
def test_multiple_waiters(self):
evt = coros.event()
evt = coros.Event()
value = 'some stuff'
results = []
def wait_on_event(i_am_done):
@@ -23,7 +23,7 @@ class TestEvent(SilencedTestCase):
waiters = []
count = 5
for i in range(count):
waiters.append(coros.event())
waiters.append(coros.Event())
api.spawn(wait_on_event, waiters[-1])
evt.send()
@@ -33,7 +33,7 @@ class TestEvent(SilencedTestCase):
self.assertEqual(len(results), count)
def test_reset(self):
evt = coros.event()
evt = coros.Event()
# calling reset before send should throw
self.assertRaises(AssertionError, evt.reset)
@@ -58,7 +58,7 @@ class TestEvent(SilencedTestCase):
self.assertEqual(evt.wait(), value2)
def test_double_exception(self):
evt = coros.event()
evt = coros.Event()
# send an exception through the event
evt.send(exc=RuntimeError('from test_double_exception'))
self.assertRaises(RuntimeError, evt.wait)
@@ -85,7 +85,7 @@ class TestActor(SilencedTestCase):
api.kill(self.actor._killer)
def test_cast(self):
evt = coros.event()
evt = coros.Event()
self.actor.cast(evt)
evt.wait()
evt.reset()
@@ -96,8 +96,8 @@ class TestActor(SilencedTestCase):
def test_cast_multi_1(self):
# make sure that both messages make it in there
evt = coros.event()
evt1 = coros.event()
evt = coros.Event()
evt1 = coros.Event()
self.actor.cast(evt)
self.actor.cast(evt1)
evt.wait()
@@ -121,17 +121,17 @@ class TestActor(SilencedTestCase):
evt.send()
self.actor.received = received
waiters.append(coros.event())
waiters.append(coros.Event())
self.actor.cast( (1, waiters[-1]))
api.sleep(0)
waiters.append(coros.event())
waiters.append(coros.Event())
self.actor.cast( (2, waiters[-1]) )
waiters.append(coros.event())
waiters.append(coros.Event())
self.actor.cast( (3, waiters[-1]) )
api.sleep(0)
waiters.append(coros.event())
waiters.append(coros.Event())
self.actor.cast( (4, waiters[-1]) )
waiters.append(coros.event())
waiters.append(coros.Event())
self.actor.cast( (5, waiters[-1]) )
for evt in waiters:
evt.wait()
@@ -148,7 +148,7 @@ class TestActor(SilencedTestCase):
self.actor.received = received
evt = coros.event()
evt = coros.Event()
self.actor.cast( ('fail', evt) )
evt.wait()
evt.reset()
@@ -168,8 +168,8 @@ class TestActor(SilencedTestCase):
def onemoment():
api.sleep(0.1)
evt = coros.event()
evt1 = coros.event()
evt = coros.Event()
evt1 = coros.Event()
self.actor.cast( (onemoment, evt, 1) )
self.actor.cast( (lambda: None, evt1, 2) )

View File

@@ -146,13 +146,13 @@ class TestDBConnectionPool(DBTester):
curs = conn.cursor()
results = []
SHORT_QUERY = "select * from test_table"
evt = coros.event()
evt = coros.Event()
def a_query():
self.assert_cursor_works(curs)
curs.execute(SHORT_QUERY)
results.append(2)
evt.send()
evt2 = coros.event()
evt2 = coros.Event()
api.spawn(a_query)
results.append(1)
self.assertEqual([1], results)
@@ -223,13 +223,13 @@ class TestDBConnectionPool(DBTester):
LONG_QUERY = "select * from test_table"
SHORT_QUERY = "select * from test_table where row_id <= 20"
evt = coros.event()
evt = coros.Event()
def long_running_query():
self.assert_cursor_works(curs)
curs.execute(LONG_QUERY)
results.append(1)
evt.send()
evt2 = coros.event()
evt2 = coros.Event()
def short_running_query():
self.assert_cursor_works(curs2)
curs2.execute(SHORT_QUERY)
@@ -373,7 +373,7 @@ class TestDBConnectionPool(DBTester):
conn = self.pool.get()
self.assertEquals(self.pool.free(), 0)
self.assertEquals(self.pool.waiting(), 0)
e = coros.event()
e = coros.Event()
def retrieve(pool, ev):
c = pool.get()
ev.send(c)

View File

@@ -58,8 +58,8 @@ class TestQueue(LimitedTestCase):
x = q.wait()
evt.send(x)
e1 = coros.event()
e2 = coros.event()
e1 = coros.Event()
e2 = coros.Event()
api.spawn(sender, e1, q)
api.sleep(0)
@@ -76,7 +76,7 @@ class TestQueue(LimitedTestCase):
evt.send(q.wait())
sendings = ['1', '2', '3', '4']
evts = [coros.event() for x in sendings]
evts = [coros.Event() for x in sendings]
for i, x in enumerate(sendings):
api.spawn(waiter, q, evts[i])
@@ -113,7 +113,7 @@ class TestQueue(LimitedTestCase):
evt.send('timed out')
evt = coros.event()
evt = coros.Event()
api.spawn(do_receive, q, evt)
self.assertEquals(evt.wait(), 'timed out')
@@ -141,8 +141,8 @@ class TestQueue(LimitedTestCase):
evt.send('timed out')
q = coros.queue()
dying_evt = coros.event()
waiting_evt = coros.event()
dying_evt = coros.Event()
waiting_evt = coros.Event()
api.spawn(do_receive, q, dying_evt)
api.spawn(waiter, q, waiting_evt)
api.sleep(0)
@@ -160,8 +160,8 @@ class TestQueue(LimitedTestCase):
evt.send('timed out')
q = coros.queue()
e1 = coros.event()
e2 = coros.event()
e1 = coros.Event()
e2 = coros.Event()
api.spawn(do_receive, q, e1)
api.spawn(do_receive, q, e2)
api.sleep(0)
@@ -176,7 +176,7 @@ class TestQueue(LimitedTestCase):
evt.send(result)
q = coros.queue()
e1 = coros.event()
e1 = coros.Event()
api.spawn(do_wait, q, e1)
api.sleep(0)
self.assertEquals(1, q.waiting())

View File

@@ -1,5 +1,5 @@
import unittest
from eventlet.coros import event
from eventlet.coros import Event
from eventlet.api import spawn, sleep, exc_after, with_timeout
from tests import LimitedTestCase
@@ -9,7 +9,7 @@ class TestEvent(LimitedTestCase):
def test_send_exc(self):
log = []
e = event()
e = Event()
def waiter():
try:
@@ -26,8 +26,8 @@ class TestEvent(LimitedTestCase):
assert log == [('catched', obj)], log
def test_send(self):
event1 = event()
event2 = event()
event1 = Event()
event2 = Event()
spawn(event1.send, 'hello event1')
exc_after(0, ValueError('interrupted'))

View File

@@ -6,7 +6,7 @@ class TestCoroutinePool(LimitedTestCase):
klass = pool.Pool
def test_execute_async(self):
done = coros.event()
done = coros.Event()
def some_work():
done.send()
pool = self.klass(0, 2)
@@ -23,7 +23,7 @@ class TestCoroutinePool(LimitedTestCase):
def test_waiting(self):
pool = self.klass(0,1)
done = coros.event()
done = coros.Event()
def consume():
done.wait()
def waiter(pool):
@@ -46,7 +46,7 @@ class TestCoroutinePool(LimitedTestCase):
self.assertEqual(pool.waiting(), 0)
def test_multiple_coros(self):
evt = coros.event()
evt = coros.Event()
results = []
def producer():
results.append('prod')
@@ -86,7 +86,7 @@ class TestCoroutinePool(LimitedTestCase):
outer_waiter = pool.execute(reenter)
outer_waiter.wait()
evt = coros.event()
evt = coros.Event()
def reenter_async():
pool.execute_async(lambda a: a, 'reenter')
evt.send('done')
@@ -99,7 +99,7 @@ class TestCoroutinePool(LimitedTestCase):
e.wait()
timer = api.exc_after(1, api.TimeoutError)
try:
evt = coros.event()
evt = coros.Event()
for x in xrange(num_free):
pool.execute(wait_long_time, evt)
# if the pool has fewer free than we expect,
@@ -119,7 +119,7 @@ class TestCoroutinePool(LimitedTestCase):
def test_resize(self):
pool = self.klass(max_size=2)
evt = coros.event()
evt = coros.Event()
def wait_long_time(e):
e.wait()
pool.execute(wait_long_time, evt)

View File

@@ -61,12 +61,12 @@ class TestProc(SilencedTestCase):
def test_event(self):
p = proc.spawn(lambda : 100)
event = coros.event()
event = coros.Event()
p.link(event)
self.assertEqual(event.wait(), 100)
for i in xrange(3):
event2 = coros.event()
event2 = coros.Event()
p.link(event2)
self.assertEqual(event2.wait(), 100)
@@ -86,7 +86,7 @@ class TestCase(SilencedTestCase):
self.p.unlink()
def set_links(self, p, first_time, kill_exc_type):
event = coros.event()
event = coros.Event()
self.link(p, event)
proc_flag = []
@@ -111,13 +111,13 @@ class TestCase(SilencedTestCase):
self.link(p, lambda *args: callback_flag.remove('initial'))
for _ in range(10):
self.link(p, coros.event())
self.link(p, coros.Event())
self.link(p, coros.queue(1))
return event, receiver, proc_flag, queue, callback_flag
def set_links_timeout(self, link):
# stuff that won't be touched
event = coros.event()
event = coros.Event()
link(event)
proc_finished_flag = []
@@ -259,11 +259,11 @@ class TestStuff(SilencedTestCase):
y = proc.spawn(lambda : 2)
z = proc.spawn(lambda : 3)
self.assertEqual(proc.waitall([x, y, z]), [1, 2, 3])
e = coros.event()
e = coros.Event()
x.link(e)
self.assertEqual(e.wait(), 1)
x.unlink(e)
e = coros.event()
e = coros.Event()
x.link(e)
self.assertEqual(e.wait(), 1)
self.assertEqual([proc.waitall([X]) for X in [x, y, z]], [[1], [2], [3]])
@@ -358,7 +358,7 @@ class TestStuff(SilencedTestCase):
self._test_multiple_listeners_error_unlink(p)
def test_killing_unlinked(self):
e = coros.event()
e = coros.Event()
def func():
try:
raise ExpectedError('test_killing_unlinked')

View File

@@ -18,7 +18,7 @@ except ImportError:
pass
from eventlet.api import spawn, sleep, with_timeout, call_after
from eventlet.coros import event
from eventlet.coros import Event
try:
from eventlet.green import socket
@@ -211,7 +211,7 @@ class TestTLSError(unittest.TestCase):
from gnutls.interfaces.twisted import X509Credentials
from gnutls.errors import GNUTLSError
cred = X509Credentials(None, None)
ev = event()
ev = Event()
def handle(conn):
ev.send("handle must not be called")
s = reactor.listenTLS(0, pr.SpawnFactory(handle, LineOnlyReceiverTransport), cred)