<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def wait(timeout=None, flush=True):
 """Wait for an event. Args: timeout (Optional[int]):
 The time in seconds that this function will wait before giving up and returning None. With the default value of None, this will block forever. flush (bool):
 If True a call to :any:`tdl.flush` will be made before listening for events. Returns: Type[Event]: An event, or None if the function has timed out. Anything added via :any:`push` will also be returned. """

    if timeout is not None:
        timeout = timeout + _time.clock() # timeout at this time
    while True:
        if _eventQueue:
            return _eventQueue.pop(0)
        if flush:
            # a full 'round' of events need to be processed before flushing
            _tdl.flush()
        if timeout and _time.clock() >= timeout:
            return None # return None on timeout
        _time.sleep(0.001) # sleep 1ms
        _processEvents()