Reputation: 14251
from gevent import monkey
monkey.patch_all()
import gevent
from gevent import pywsgi
from gevent import queue
import redis
REDIS_CONNECTION_POOL = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB)
def redis_wait(environ, body, channel, wait_once):
server = redis.Redis(connection_pool=REDIS_CONNECTION_POOL)
client = server.pubsub()
client.subscribe(channel)
messages = client.listen()
while True:
message = messages.next()
This is the error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gevent/greenlet.py", line 327, in run
result = self._run(*self.args, **self.kwargs)
File "/home/ubuntu/www/app.wsgi", line 110, in wait_messages
redis_wait(environ, body, channel, False)
File "/home/ubuntu/www/app.wsgi", line 47, in redis_wait
message = messages.next()
StopIteration
<Greenlet at 0x1c190f0: wait_messages({}, ['5386E49C1CEB16573ACBD90566F3B740983768CB,1358532, <Queue at 0x19fd6d0>, '1410290151', None)> failed with StopIteration
I have tried to google the error, but nothing comes up. The error only occurs intermittently. Does anyone know what it means? Is it a timeout of some sort perhaps?
Upvotes: 0
Views: 218
Reputation: 36698
StopIteration
is the exception that Python throws when an iterator (such as messages
) has reached the end of its values. It is not an error, but a normal, expected condition that will be automatically handled by Python in some circumstances. For example, if you loop over the iterator using a for
loop like so:
for message in messages:
print message # Or do something with it
then the StopIteration
exception will end the for
loop normally.
However, a while
loop does not handle StopIteration
itself, but lets it continue through to your code so that you can handle it in whatever way you see fit. Your code is currently not handling it, so the exception ends up terminating your program.
Replace your while True: message = messages.next()
loop with for message in messages
and everything should work.
More reading on iterators, generators, and StopIteration
:
Upvotes: 1