Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import asyncio
  2. import heapq
  3.  
  4. class StopSim(BaseException):
  5. """Raised to stop the simulation"""
  6.  
  7.  
  8. class SimEventLoop(asyncio.AbstractEventLoop):
  9. def __init__(self):
  10. self._time = 0
  11. self._scheduled = []
  12. self._ready = []
  13. self._debug = False
  14.  
  15. def get_debug(self):
  16. return self._debug
  17.  
  18. def create_task(self, coro):
  19. task = asyncio.Task(coro, loop=self)
  20. return task
  21.  
  22. def time(self):
  23. return self._time
  24.  
  25. def call_soon(self, callback, *args):
  26. # print('call soon', callback)
  27. handle = asyncio.Handle(callback, args, self)
  28. handle._run()
  29.  
  30. def call_later(self, delay, callback, *args):
  31. # print('call_later', delay, callback)
  32. timer = self.call_at(self.time() + delay, callback, *args)
  33. return timer
  34.  
  35. def call_at(self, when, callback, *args):
  36. # print('call_at', when, callback)
  37. timer = asyncio.TimerHandle(when, callback, args, self)
  38. heapq.heappush(self._scheduled, timer)
  39. timer._scheduled = True
  40. return timer
  41.  
  42. def _timer_handle_cancelled(self, handle):
  43. # print('hc')
  44. pass
  45.  
  46. def _run_once(self):
  47. # print(self.time(), self._scheduled)
  48. while self._scheduled:
  49. handle = self._scheduled[0]
  50. if handle._when > self._time:
  51. break
  52. handle = heapq.heappop(self._scheduled)
  53. handle._scheduled = False
  54. handle._run()
  55. self._time += 1
  56.  
  57. def run_forever(self):
  58. while True:
  59. try:
  60. self._run_once()
  61. except StopSim:
  62. break
  63.  
  64.  
  65. def run_until_complete(self, future):
  66. new_task = not isinstance(future, asyncio.Future)
  67. future = asyncio.ensure_future(future, loop=self)
  68.  
  69. if new_task:
  70. future._log_destroy_pending = False
  71.  
  72. def lol(fut):
  73. raise StopSim
  74. # print('done')
  75.  
  76. future.add_done_callback(lol)
  77.  
  78. try:
  79. self.run_forever()
  80. except:
  81. if new_task and future.done() and not future.cancelled():
  82. print('canc')
  83. future.exception()
  84. raise
  85. future.remove_done_callback(lol)
  86.  
  87. return future.result()
  88.  
  89. def delay(time, result=None, *, loop=None):
  90. async def _delay():
  91. """Coroutine that completes after a given time (in seconds)."""
  92. # based on asyncio.sleep
  93. future = asyncio.Future(loop=loop)
  94. h = future._loop.call_later(time,
  95. future._set_result_unless_cancelled, result)
  96. try:
  97. return (await future)
  98. finally:
  99. h.cancel()
  100.  
  101. return _delay
  102.  
  103. def now(loop=None):
  104. if loop is None:
  105. loop = asyncio.get_event_loop()
  106. return loop.time()
  107.  
  108. def always(cond):
  109. def decorator(func):
  110. async def wrapper():
  111. while True:
  112. await cond()
  113. func()
  114. return wrapper
  115. return decorator
  116.  
  117. @always(delay(3))
  118. def hi():
  119. t = asyncio.get_event_loop().time()
  120. print(now(), 'hi')
  121.  
  122. def main():
  123. loop = SimEventLoop()
  124. asyncio.set_event_loop(loop)
  125. loop.run_until_complete(hi())
  126.  
  127. if __name__ == '__main__':
  128. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement