diff --git a/dddp/__init__.py b/dddp/__init__.py index d93af05..0047026 100644 --- a/dddp/__init__.py +++ b/dddp/__init__.py @@ -5,6 +5,7 @@ from pkg_resources import get_distribution, DistributionNotFound from gevent.local import local from dddp import alea + try: _dist = get_distribution('django-ddp') if not __file__.startswith(os.path.join(_dist.location, 'django-ddp', '')): @@ -18,6 +19,18 @@ else: default_app_config = 'dddp.apps.DjangoDDPConfig' +def greenify(): + """Patch threading and psycopg2 modules for green threads.""" + if 'threading' in sys.modules: + raise Exception('threading module loaded before patching!') + + from gevent.monkey import patch_all + patch_all() + + from psycogreen.gevent import patch_psycopg + patch_psycopg() + + class AlreadyRegistered(Exception): """Raised when registering over the top of an existing registration.""" diff --git a/dddp/main.py b/dddp/main.py index 3b11ac8..f6b0b39 100644 --- a/dddp/main.py +++ b/dddp/main.py @@ -9,18 +9,6 @@ import sys Addr = collections.namedtuple('Addr', ['host', 'port']) -def greenify(): - """Patch threading and psycopg2 modules for green threads.""" - if 'threading' in sys.modules: - raise Exception('threading module loaded before patching!') - - from gevent.monkey import patch_all - patch_all() - - from psycogreen.gevent import patch_psycopg - patch_psycopg() - - def ddpp_sockjs_xhr(environ, start_response): """Dummy method that doesn't handle XHR requests.""" start_response( @@ -200,10 +188,10 @@ def main(): 'listen', metavar='address[:port]', nargs='*', type=addr, ) namespace = parser.parse_args() - # monkey patch stdlib for gevent. - greenify() serve(namespace.listen or [Addr('localhost', 8000)]) if __name__ == '__main__': + from dddp import greenify + greenify() main() diff --git a/test_project/manage.py b/test_project/manage.py index 0fc36a3..ad43cfd 100755 --- a/test_project/manage.py +++ b/test_project/manage.py @@ -2,9 +2,13 @@ import os import sys + if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test_project.settings") + from dddp import greenify + greenify() + from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)