request_handler.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import sys
  2. import modelverse_kernel.primitives as primitive_functions
  3. import modelverse_kernel.jit as jit
  4. from collections import defaultdict
  5. class KnownRequestHandled(Exception):
  6. """An exception that signifies that a known request was handled."""
  7. pass
  8. class UnhandledRequestHandlerException(Exception):
  9. """The type of exception that is thrown when the request handler encounters an
  10. unhandled exception."""
  11. def __init__(self, inner_exception, stack_trace):
  12. import traceback
  13. Exception.__init__(
  14. self,
  15. """The request handler encountered an unknown exception.\n
  16. Inner exception: %s\n
  17. """ % (traceback.format_exc()))
  18. self.inner_exception = inner_exception
  19. self.stack_trace = stack_trace
  20. class RequestHandler(object):
  21. """A type of object that intercepts logic-related Modelverse requests, and
  22. forwards Modelverse state requests."""
  23. def __init__(self):
  24. # generator_stack is a stack of GeneratorStackEntry values.
  25. self.generator_stack = []
  26. # exception_handlers is a stack of
  27. # (generator_stack index, [(exception type, handler function)])
  28. # tuples.
  29. self.produce_stack_trace = True
  30. self.handlers = {
  31. 'CALL' : self.execute_call,
  32. 'CALL_ARGS' : self.execute_call_args,
  33. 'CALL_KWARGS' : self.execute_call_kwargs,
  34. 'SLEEP' : self.execute_sleep,
  35. }
  36. def handle_request(self, reply):
  37. """Replies to a request from the top-of-stack generator, and returns a new request."""
  38. if not self.generator_stack:
  39. raise ValueError('handle_request cannot be called with an empty generator stack.')
  40. # Append the server's replies to the list of replies.
  41. if reply is not None:
  42. gen = self.generator_stack[-1]
  43. if gen["replies"]:
  44. gen["replies"].extend(reply)
  45. else:
  46. gen["replies"] = reply
  47. while 1:
  48. # Silence pylint's warning about catching Exception.
  49. # pylint: disable=I0011,W0703
  50. try:
  51. gen = self.generator_stack[-1]
  52. if gen["finished_requests"]:
  53. gen["pending_requests"] = gen["generator"].send(gen["replies"])
  54. gen["finished_requests"] = False
  55. gen["replies"] = None
  56. ret = self.pop_requests()
  57. if ret[0]:
  58. return ret[1]
  59. except StopIteration:
  60. # Done, so remove the generator
  61. del self.generator_stack[-1]
  62. if self.generator_stack:
  63. # This generator was called from another generator.
  64. # Append 'None' to the caller's list of replies.
  65. self.append_reply(None)
  66. else:
  67. # Looks like we're done here.
  68. return None
  69. except primitive_functions.PrimitiveFinished as ex:
  70. # Done, so remove the generator
  71. del self.generator_stack[-1]
  72. if self.generator_stack:
  73. # This generator was called from another generator.
  74. # Append the callee's result to the caller's list of replies.
  75. self.append_reply(ex.result)
  76. else:
  77. # Looks like we're done here.
  78. return None
  79. def push_generator(self, gen):
  80. """Pushes a new generator onto the stack."""
  81. dd = defaultdict(lambda : None)
  82. dd["generator"] = gen
  83. dd["finished_requests"] = True
  84. self.generator_stack.append(dd)
  85. # print('Pushed generator %s. Generator count: %d' % (gen, len(self.generator_stack)))
  86. def append_reply(self, new_reply):
  87. """Appends a reply to the top-of-stack generator's list of pending replies."""
  88. if self.generator_stack[-1]["replies"] is None:
  89. self.generator_stack[-1]["replies"] = [new_reply]
  90. else:
  91. self.generator_stack[-1]["replies"].append(new_reply)
  92. def pop_requests(self):
  93. """Tries to pop a batch of Modelverse _state_ requests from the
  94. current list of requests. Known requests are executed immediately.
  95. A list of requests and a Boolean are returned. The latter is True
  96. if there are no more requests to process, and false otherwise."""
  97. requests = self.generator_stack[-1]["pending_requests"]
  98. if requests:
  99. if requests[0][0] in self.handlers:
  100. # First element is a known request
  101. elem = requests.pop(0)
  102. # The list of requests might be empty now. If so, then flag this
  103. # batch of requests as finished.
  104. if not requests:
  105. self.generator_stack[-1]["finished_requests"] = True
  106. # Handle the request.
  107. self.handlers[elem[0]](elem[1])
  108. return (False, None)
  109. #raise KnownRequestHandled()
  110. else:
  111. for i, elem in enumerate(requests):
  112. if elem[0] in self.handlers:
  113. # Handle any requests that precede the known request first.
  114. pre_requests = requests[:i]
  115. del requests[:i]
  116. return pre_requests
  117. # We couldn't find a known request in the batch of requests, so we might as well
  118. # handle them all at once then.
  119. self.generator_stack[-1]["finished_requests"] = True
  120. #return requests
  121. return (True, requests)
  122. def execute_call(self, request_args):
  123. """Executes a CALL-request with the given argument list."""
  124. # Format: ("CALL", [gen])
  125. gen, = request_args
  126. self.push_generator(gen)
  127. def execute_call_kwargs(self, request_args):
  128. """Executes a CALL_KWARGS-request with the given argument list."""
  129. # Format: ("CALL_KWARGS", [func, kwargs])
  130. # This format is useful because it also works for functions that
  131. # throw an exception but never yield.
  132. func, kwargs = request_args
  133. # We need to be extra careful here, because func(**kwargs) might
  134. # not be a generator at all: it might simply be a method that
  135. # raises an exception. To cope with this we need to push a dummy
  136. # entry onto the stack if a StopIteration or PrimtiveFinished
  137. # exception is thrown. The logic in execute_yields will then pop
  138. # that dummy entry.
  139. try:
  140. self.push_generator(func(**kwargs))
  141. except StopIteration:
  142. self.push_generator(None)
  143. raise
  144. except primitive_functions.PrimitiveFinished:
  145. self.push_generator(None)
  146. raise
  147. except:
  148. print("EXCEPTION for " + str(locals()))
  149. raise
  150. def execute_call_args(self, request_args):
  151. """Executes a CALL_ARGS-request with the given argument list."""
  152. # Format: ("CALL_ARGS", [gen, args])
  153. func, args = request_args
  154. # We need to be extra careful here, because func(*args) might
  155. # not be a generator at all: it might simply be a method that
  156. # raises an exception. To cope with this we need to push a dummy
  157. # entry onto the stack if a StopIteration or PrimtiveFinished
  158. # exception is thrown. The logic in execute_yields will then pop
  159. # that dummy entry.
  160. try:
  161. self.push_generator(func(*args))
  162. except StopIteration:
  163. self.push_generator(None)
  164. raise
  165. except primitive_functions.PrimitiveFinished:
  166. self.push_generator(None)
  167. raise
  168. def execute_sleep(self, request_args):
  169. """Executes a SLEEP-request with the given argument list."""
  170. self.append_reply(None)
  171. raise primitive_functions.SleepKernel(request_args[0], request_args[1])