PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/slpmonkeypatch/__init__.py

http://stacklessexamples.googlecode.com/
Python | 43 lines | 30 code | 7 blank | 6 comment | 2 complexity | 8c709548e8342d2eb8dbf9250f4c9b25 MD5 | raw file
  1. import os
  2. import sys
  3. import stackless
  4. import threading
  5. import socket as stdsocket
  6. def register_thread_for_stackless():
  7. _config = getattr(stackless, "_config", None)
  8. if _config is None:
  9. _config = stackless._config = threading.local()
  10. _config.using_stackless = True
  11. _config.emulation_timeout = 0.1
  12. def monkeypatch():
  13. # This thread is the only automatically registered one.
  14. stackless.register_thread = register_thread_for_stackless
  15. stackless.register_thread()
  16. # Go through some shenanigans to get the path to the 'examples'
  17. # directory in the SVN 'sandbox' directory structure.
  18. filePath = os.path.join(os.getcwd(), __file__)
  19. dirPath = os.path.split(filePath)[0]
  20. dirPath = os.path.join(dirPath, os.path.pardir)
  21. dirPath = os.path.join(dirPath, os.path.pardir)
  22. dirPath = os.path.normpath(dirPath)
  23. dirPath = os.path.join(dirPath, "examples")
  24. # Add the 'examples' directory to the import path.
  25. if dirPath not in sys.path:
  26. sys.path.append(dirPath)
  27. # Install the import reference to the normal python socket module.
  28. import socket
  29. sys.modules["stdsocket"] = __import__(socket.__name__)
  30. del socket
  31. # Obtain the asyncore-based socket module and put its 'socket' in place.
  32. import stacklesssocket
  33. stackless.socket = stacklesssocket.socket
  34. del stacklesssocket
  35. import socketmodule
  36. sys.modules["socket"] = socketmodule