Skip to content

Commit

Permalink
Avoid await (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin Hugo <[email protected]>
  • Loading branch information
Athanaseus and bennahugo authored Jan 6, 2021
1 parent 29f7821 commit aa8c1bb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
22 changes: 13 additions & 9 deletions OCTOPython/src/octopussy.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ def event_loop (self,await_=[],timeout=None):
limit, use None to loop indefinitely (or until the C++ WP has exited). If
timeout=0, processes all pending messages and returns.
""";
# convert await argument to list of hiids

# convert await_ argument to list of hiids
await_ = make_hiid_list(await_);
_dprint(1,"running event loop, timeout",timeout,"await_",await_);
_dprint(1,"running event loop, timeout",timeout,"await",await_);

if timeout is None:
endtime = 1e+40; # quite long enough...
else:
Expand Down Expand Up @@ -342,9 +344,10 @@ def event_loop (self,await_=[],timeout=None):
# end of while-loop, if we dropped out, it's a timeout, return None
return None

def await_ (self,what,timeout=None,resume=False):
"""alias for event_loop() with an await_ argument.
if resume is true, resumes the event loop before commencing await_. This

def await_(self,what,timeout=None,resume=False):
"""alias for event_loop() with an await argument.
if resume is true, resumes the event loop before commencing await. This
is meant for child classes only.
""";
return self.event_loop(await_=what,timeout=timeout);
Expand Down Expand Up @@ -412,7 +415,7 @@ def lock (self):

# this is meant to pause and resume event processing -- no implementation
# needed since threads don't actually work for now (i.e., events are
# not being deal with outside await/event_loop calls)
# not being deal with outside await_/event_loop calls)

def pause_events (self):
"""pauses the event loop for this wp (if any); this will halt the"
Expand All @@ -434,7 +437,8 @@ def resume_events (self):

# await_ blocks until the specified message has been received
# (with optional timeout)
def await_ (self,what,timeout=None,resume=False):

def await_(self,what,timeout=None,resume=False):
cur_thread = self._api.currentThread();
if cur_thread is self._thread:
raise AssertionError("can't call await_() from event handler thread");
Expand Down Expand Up @@ -595,8 +599,8 @@ def run (self):

print("awaiting on wp4...");
res = wp4.await_("reflect.*",resume=True);
print(("await_ result: ",res));


print(("await result: ",res));
print("=== (3) ===");

print(('wp1 queue: ',wp1.num_pending()));
Expand Down
7 changes: 4 additions & 3 deletions PyApps/src/Apps/app_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,8 @@ def ensure_connection (self,wait=True):
while self.state is None:
self.dprint(2,'no connection to app, awaiting (wait=',wait,')');
res = self._pwp.await_('*',resume=True,timeout=5); # await anything, but keep looping until status changes
self.dprint(3,'await_ returns',res);
self.dprint(3,'await returns',res);

if time.time() >= endtime:
raise RuntimeError("timeout waiting for connection");
finally:
Expand Down Expand Up @@ -404,8 +405,8 @@ def whenever (self,*args,**kwargs):
args = (self._rcv_prefix + args[0],) + args[1:];
return self._pwp.whenever(*args,**kwargs);

def await_ (self,what,timeout=None,resume=False):
"interface to pwp's event loop, in the await_ form";
def await_(self,what,timeout=None,resume=False):
"interface to pwp's event loop, in the await form";
if timeout is not None:
await_timeout = min(1,timeout);
timeout = time.time() + timeout;
Expand Down
7 changes: 4 additions & 3 deletions PyApps/src/Apps/multiapp_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def ensure_connection (self,wait=True):
while not self.current_server:
self.dprint(2,'no connection to servers, awaiting (wait=',wait,')');
res = self._pwp.await_('*',resume=True,timeout=5); # await anything, but keep looping until status changes
self.dprint(3,'await_ returns',res);
self.dprint(3,'await returns',res);
if time.time() >= endtime:
raise RuntimeError("timeout waiting for connection");
finally:
Expand Down Expand Up @@ -515,8 +515,9 @@ def whenever (self,*args,**kwargs):
args = (self._rcv_prefix + args[0],) + args[1:];
return self._pwp.whenever(*args,**kwargs);

def await_ (self,what,timeout=None,resume=False):
"interface to pwp's event loop, in the await_ form";

def await_(self,what,timeout=None,resume=False):
"interface to pwp's event loop, in the await form";
if timeout is not None:
await_timeout = min(1,timeout);
timeout = time.time() + timeout;
Expand Down

0 comments on commit aa8c1bb

Please sign in to comment.