You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 line
2.0 KiB

  1. import asyncio
  2. import sys
  3. def _format_coroutine(coro):
  4. if asyncio.iscoroutine(coro) \
  5. and not hasattr(coro, 'cr_code') \
  6. and not hasattr(coro, 'gi_code'):
  7. # Most likely a Cython coroutine
  8. coro_name = '{}()'.format(coro.__qualname__ or coro.__name__)
  9. running = False
  10. try:
  11. running = coro.cr_running
  12. except AttributeError:
  13. try:
  14. running = coro.gi_running
  15. except AttributeError:
  16. pass
  17. if running:
  18. return '{} running'.format(coro_name)
  19. else:
  20. return coro_name
  21. return _old_format_coroutine(coro)
  22. async def _wait_for_data(self, func_name):
  23. """Wait until feed_data() or feed_eof() is called.
  24. If stream was paused, automatically resume it.
  25. """
  26. if self._waiter is not None:
  27. raise RuntimeError('%s() called while another coroutine is '
  28. 'already waiting for incoming data' % func_name)
  29. assert not self._eof, '_wait_for_data after EOF'
  30. # Waiting for data while paused will make deadlock, so prevent it.
  31. if self._paused:
  32. self._paused = False
  33. self._transport.resume_reading()
  34. try:
  35. create_future = self._loop.create_future
  36. except AttributeError:
  37. self._waiter = asyncio.Future(loop=self._loop)
  38. else:
  39. self._waiter = create_future()
  40. try:
  41. await self._waiter
  42. finally:
  43. self._waiter = None
  44. if sys.version_info < (3, 5, 3):
  45. # This is needed to support Cython 'async def' coroutines.
  46. from asyncio import coroutines
  47. _old_format_coroutine = coroutines._format_coroutine
  48. coroutines._format_coroutine = _format_coroutine
  49. if sys.version_info < (3, 5, 2):
  50. # Fix a possible deadlock, improve performance.
  51. from asyncio import streams
  52. _old_wait_for_data = streams.StreamReader._wait_for_data
  53. _wait_for_data.__module__ = _old_wait_for_data.__module__
  54. streams.StreamReader._wait_for_data = _wait_for_data