reactor_magic.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # The contents of this file are subject to the Python Software Foundation
  2. # License Version 2.3 (the License). You may not copy or use this file, in
  3. # either source code or executable form, except in compliance with the License.
  4. # You may obtain a copy of the License at http://www.python.org/license.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. import os
  11. import sys
  12. try:
  13. from resource import setrlimit, getrlimit, RLIMIT_NOFILE
  14. try:
  15. setrlimit(RLIMIT_NOFILE, (100000, 100000))
  16. except ValueError, e:
  17. # dont be so noisy here
  18. pass
  19. # print ">>> unable to setrlimit ", e
  20. except ImportError, e:
  21. pass
  22. if 'twisted.internet.reactor' in sys.modules:
  23. print ("twisted.internet.reactor was imported before BTL.reactor_magic!\n"
  24. "I'll clean it up for you, but don't do that!\n"
  25. "Existing reference may be for the wrong reactor!\n"
  26. "!")
  27. del sys.modules['twisted.internet.reactor']
  28. noSignals = True
  29. is_iocpreactor = False
  30. if os.name == 'nt':
  31. try:
  32. from BTL.Luciana import reactor
  33. noSignals = False
  34. is_iocpreactor = True
  35. except:
  36. pass
  37. else:
  38. try:
  39. from twisted.internet import kqreactor
  40. kqreactor.install()
  41. except:
  42. try:
  43. from BTL import epollreactor
  44. epollreactor.install()
  45. except:
  46. try:
  47. from twisted.internet import pollreactor
  48. pollreactor.install()
  49. except:
  50. pass
  51. from twisted.internet import reactor
  52. old_run = reactor.run
  53. def run_default(method=None, **kw):
  54. if method:
  55. reactor.callLater(0, method)
  56. return old_run(**kw)
  57. reactor.run = run_default