Fix the Waiter handler for asyncio
This commit is contained in:
parent
6b1a04f59d
commit
e7248d9af9
@ -10,8 +10,10 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from queue import Queue, Empty
|
import asyncio
|
||||||
|
from asyncio import Queue, wait_for, TimeoutError
|
||||||
|
|
||||||
|
import slixmpp
|
||||||
from slixmpp.xmlstream.handler.base import BaseHandler
|
from slixmpp.xmlstream.handler.base import BaseHandler
|
||||||
|
|
||||||
|
|
||||||
@ -42,12 +44,13 @@ class Waiter(BaseHandler):
|
|||||||
:param payload: The matched
|
:param payload: The matched
|
||||||
:class:`~slixmpp.xmlstream.stanzabase.ElementBase` object.
|
:class:`~slixmpp.xmlstream.stanzabase.ElementBase` object.
|
||||||
"""
|
"""
|
||||||
self._payload.put(payload)
|
self._payload.put_nowait(payload)
|
||||||
|
|
||||||
def run(self, payload):
|
def run(self, payload):
|
||||||
"""Do not process this handler during the main event loop."""
|
"""Do not process this handler during the main event loop."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
def wait(self, timeout=None):
|
def wait(self, timeout=None):
|
||||||
"""Block an event handler while waiting for a stanza to arrive.
|
"""Block an event handler while waiting for a stanza to arrive.
|
||||||
|
|
||||||
@ -63,18 +66,13 @@ class Waiter(BaseHandler):
|
|||||||
value.
|
value.
|
||||||
"""
|
"""
|
||||||
if timeout is None:
|
if timeout is None:
|
||||||
timeout = self.stream().response_timeout
|
timeout = slixmpp.xmlstream.RESPONSE_TIMEOUT
|
||||||
|
|
||||||
elapsed_time = 0
|
stanza = None
|
||||||
stanza = False
|
try:
|
||||||
while elapsed_time < timeout and not self.stream().stop.is_set():
|
stanza = yield from self._payload.get()
|
||||||
try:
|
except TimeoutError:
|
||||||
stanza = self._payload.get(True, 1)
|
log.warning("Timed out waiting for %s", self.name)
|
||||||
break
|
|
||||||
except Empty:
|
|
||||||
elapsed_time += 1
|
|
||||||
if elapsed_time >= timeout:
|
|
||||||
log.warning("Timed out waiting for %s", self.name)
|
|
||||||
self.stream().remove_handler(self.name)
|
self.stream().remove_handler(self.name)
|
||||||
return stanza
|
return stanza
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user