title: How to send requests while receiving responses in IB API?
body: <p>I don't quite understand how to go about using IB API Wrapper and Client streams. I understand that I am sending out requests to the IB server through the Client stream and am receiving the response through the Wrapper stream, but I would like to be able to implement a menu for the user such as "Press 'p' for open positions, etc." followed by a display of the server response and a prompt for another action.</p>
  
  <pre><code>from ibapi import wrapper
  from ibapi.client import EClient
  from ibapi.utils import iswrapper
  
  # types
  from ibapi.common import *
  from ibapi.contract import *
  
  
  class TestClient(EClient):
      def __init__(self, wrapper):
          EClient.__init__(self, wrapper)
  
  
  class TestWrapper(wrapper.EWrapper):
      def __init__(self):
          wrapper.EWrapper.__init__(self)
  
  
  class TestApp(TestWrapper, TestClient):
      def __init__(self):
          TestWrapper.__init__(self)
          TestClient.__init__(self, wrapper=self)
  
      def position(self, account: str, contract: Contract, position: float,
                   avgCost: float):
          """This event returns real-time positions for all accounts in
          response to the reqPositions() method."""
  
          super().position(account, contract, position, avgCost)
          print("Position.", account, "Symbol:", contract.symbol, "SecType:",
                contract.secType, "Currency:", contract.currency,
                "Position:", position, "Avg cost:", avgCost)
  
  
  def main():
      app = TestApp()
      usr_in = ''
  
      app.connect("127.0.0.1", 7497, clientId=0)
      print("serverVersion:%s connectionTime:%s" % (app.serverVersion(),
                                                app.twsConnectionTime()))
  
      while(usr_in != 'exit'):
           usr_in = input('What to do next?')
           if usr_in == 'p':
               app.reqPositions()
  
      app.run()
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>What ends up happening now is that the program asks for input, but does not display the server response until I exit the loop, after which it does display the response. I know I can include some logic within the position() function, but this is very limiting and there must be a better way to interact with the two streams. Also, a pointer to any further readings on the topic of streams like the ones used in IB API will be greatly appreciated.</p>