__init__.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details.
  2. """
  3. gevent is a coroutine-based Python networking library that uses greenlet
  4. to provide a high-level synchronous API on top of libev event loop.
  5. See http://www.gevent.org/ for the documentation.
  6. """
  7. from __future__ import absolute_import
  8. from collections import namedtuple
  9. _version_info = namedtuple('version_info',
  10. ('major', 'minor', 'micro', 'releaselevel', 'serial'))
  11. #: The programatic version identifier. The fields have (roughly) the
  12. #: same meaning as :data:`sys.version_info`
  13. #: Deprecated in 1.2.
  14. version_info = _version_info(1, 2, 2, 'dev', 0)
  15. #: The human-readable PEP 440 version identifier
  16. __version__ = '1.2.2'
  17. __all__ = ['get_hub',
  18. 'Greenlet',
  19. 'GreenletExit',
  20. 'spawn',
  21. 'spawn_later',
  22. 'spawn_raw',
  23. 'iwait',
  24. 'wait',
  25. 'killall',
  26. 'Timeout',
  27. 'with_timeout',
  28. 'getcurrent',
  29. 'sleep',
  30. 'idle',
  31. 'kill',
  32. 'signal',
  33. 'fork',
  34. 'reinit']
  35. import sys
  36. if sys.platform == 'win32':
  37. # trigger WSAStartup call
  38. import socket # pylint:disable=unused-import,useless-suppression
  39. del socket
  40. from gevent.hub import get_hub, iwait, wait
  41. from gevent.greenlet import Greenlet, joinall, killall
  42. joinall = joinall # export for pylint
  43. spawn = Greenlet.spawn
  44. spawn_later = Greenlet.spawn_later
  45. from gevent.timeout import Timeout, with_timeout
  46. from gevent.hub import getcurrent, GreenletExit, spawn_raw, sleep, idle, kill, reinit
  47. try:
  48. from gevent.os import fork
  49. except ImportError:
  50. __all__.remove('fork')
  51. # See https://github.com/gevent/gevent/issues/648
  52. # A temporary backwards compatibility shim to enable users to continue
  53. # to treat 'from gevent import signal' as a callable, to matter whether
  54. # the 'gevent.signal' module has been imported first
  55. from gevent.hub import signal as _signal_class
  56. from gevent import signal as _signal_module
  57. # The object 'gevent.signal' must:
  58. # - be callable, returning a gevent.hub.signal;
  59. # - answer True to isinstance(gevent.signal(...), gevent.signal);
  60. # - answer True to isinstance(gevent.signal(...), gevent.hub.signal)
  61. # - have all the attributes of the module 'gevent.signal';
  62. # - answer True to isinstance(gevent.signal, types.ModuleType) (optional)
  63. # The only way to do this is to use a metaclass, an instance of which (a class)
  64. # is put in sys.modules and is substituted for gevent.hub.signal.
  65. # This handles everything except the last one.
  66. class _signal_metaclass(type):
  67. def __getattr__(cls, name):
  68. return getattr(_signal_module, name)
  69. def __setattr__(cls, name, value):
  70. setattr(_signal_module, name, value)
  71. def __instancecheck__(cls, instance):
  72. return isinstance(instance, _signal_class)
  73. def __dir__(cls):
  74. return dir(_signal_module)
  75. class signal(object):
  76. __doc__ = _signal_module.__doc__
  77. def __new__(cls, *args, **kwargs):
  78. return _signal_class(*args, **kwargs)
  79. # The metaclass is applied after the class declaration
  80. # for Python 2/3 compatibility
  81. signal = _signal_metaclass(str("signal"),
  82. (),
  83. dict(signal.__dict__))
  84. sys.modules['gevent.signal'] = signal
  85. sys.modules['gevent.hub'].signal = signal
  86. del sys
  87. # the following makes hidden imports visible to freezing tools like
  88. # py2exe. see https://github.com/gevent/gevent/issues/181
  89. def __dependencies_for_freezing():
  90. # pylint:disable=unused-variable
  91. from gevent import core
  92. from gevent import resolver_thread
  93. from gevent import resolver_ares
  94. from gevent import socket as _socket
  95. from gevent import threadpool
  96. from gevent import thread
  97. from gevent import threading
  98. from gevent import select
  99. from gevent import subprocess
  100. import pprint
  101. import traceback
  102. import signal as _signal
  103. del __dependencies_for_freezing