Skip to content

Commit c929e4d

Browse files
authored
some tune ups (#589)
* added option to skip metadata parsing as this can be slow for large npar/nobs * more tune ups in pypestworker * more worker stuff
1 parent dc70aa1 commit c929e4d

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

pyemu/pst/pst_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Pst(object):
6262
6363
"""
6464

65+
6566
def __init__(self, filename, load=True, resfile=None, parse_metadata=True,
6667
result_dir=None):
6768

@@ -141,6 +142,7 @@ def __init__(self, filename, load=True, resfile=None, parse_metadata=True,
141142
raise Exception("pst file not found:{0}".format(filename))
142143

143144
self.load(filename, parse_metadata=parse_metadata)
145+
144146
if result_dir is None and os.path.exists(filename):
145147
result_dir = os.path.split(os.path.abspath(filename))[0]
146148

pyemu/utils/os_utils.py

+27-13
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def send(self,s,mtype,group,runid,desc,data):
651651
full_desc = desc + fill_desc
652652
buf += full_desc.encode()
653653
buf += sdata
654-
s.send(buf)
654+
s.sendall(buf)
655655

656656

657657
def _check_sec_message(self,recv_sec_message):
@@ -660,9 +660,21 @@ def _check_sec_message(self,recv_sec_message):
660660
format(recv_sec_message,self.sec_message))
661661

662662
class PyPestWorker(object):
663+
"""a pure python worker for pest++. the pest++ master doesnt even know...
663664
665+
Args:
666+
pst (str or pyemu.Pst): something about a control file
667+
host (str): master hostname or IPv4 address
668+
port (int): port number that the master is listening on
669+
timeout (float): number of seconds to sleep at different points in the process.
670+
if you have lots of pars and/obs, a longer sleep can be helpful, but if you make this smaller,
671+
the worker responds faster...'it depends'
672+
verbose (bool): flag to echo what's going on to stdout
673+
socket_timeout (float): number of seconds that the socket should wait before giving up.
674+
generally, this can be a big number...
675+
"""
664676

665-
def __init__(self, pst, host, port, timeout=0.1,verbose=True):
677+
def __init__(self, pst, host, port, timeout=0.25,verbose=True, socket_timeout=None):
666678
self.host = host
667679
self.port = port
668680
self._pst_arg = pst
@@ -673,7 +685,9 @@ def __init__(self, pst, host, port, timeout=0.1,verbose=True):
673685
self.verbose = bool(verbose)
674686
self.par_names = None
675687
self.obs_names = None
676-
688+
if socket_timeout is None:
689+
socket_timeout = timeout * 100
690+
self.socket_timeout = socket_timeout
677691
self.par_values = None
678692
self.max_reconnect_attempts = 10
679693
self._process_pst()
@@ -695,23 +709,19 @@ def _process_pst(self):
695709

696710

697711
def connect(self,is_reconnect=False):
698-
self.message("trying to connect to {0}:{1}...".format(self.host,self.port))
712+
self.message("trying to connect to {0}:{1}...".format(self.host,self.port),echo=True)
699713
self.s = None
700714
c = 0
701715
while True:
702716
try:
703717
time.sleep(self.timeout)
704-
print(".", end='')
705718
c += 1
706-
if c % 75 == 0:
707-
print('')
708-
print(c)
709719
if is_reconnect and c > self.max_reconnect_attempts:
710720
print("max reconnect attempts reached...")
711721
return False
712722
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
713723
self.s.connect((self.host, self.port))
714-
self.message("connected to {0}:{1}".format(self.host,self.port))
724+
self.message("connected to {0}:{1}".format(self.host,self.port),echo=True)
715725
break
716726

717727
except ConnectionRefusedError:
@@ -723,8 +733,8 @@ def connect(self,is_reconnect=False):
723733
return True
724734

725735

726-
def message(self,msg):
727-
if self.verbose:
736+
def message(self,msg,echo=False):
737+
if self.verbose or echo:
728738
print(str(datetime.now())+" : "+msg)
729739

730740

@@ -745,7 +755,7 @@ def send(self,mtype,group,runid,desc="",data=0):
745755
return True
746756

747757
def listen(self,lock=None,send_lock=None):
748-
self.s.settimeout(self.timeout)
758+
self.s.settimeout(self.socket_timeout)
749759
failed_reconnect = False
750760
while True:
751761
time.sleep(self.timeout)
@@ -757,9 +767,13 @@ def listen(self,lock=None,send_lock=None):
757767
if not success:
758768
print("...exiting")
759769
time.sleep(self.timeout)
770+
# set the teminate flag so that the get_pars() look will exit
771+
self._lock.acquire()
772+
self.net_pack.mtype = 14
773+
self._lock.release()
760774
return
761775
else:
762-
print("...reconnect successfully...")
776+
print("...reconnected successfully...")
763777
continue
764778

765779
if n > 0:

0 commit comments

Comments
 (0)