1- import time
1+ import time # noqa: F401
22import logging
33import os
44import atexit
5- from ast import literal_eval
5+ from ast import literal_eval # noqa: F401
66import sys
7- import re
8- import zmq
9- import numpy as np
7+ import re # noqa: F401
8+ import zmq # noqa: F401
9+ import numpy as np # noqa: F401
1010import signal
1111
1212import concore_base
1313
14- logger = logging .getLogger (' concore' )
14+ logger = logging .getLogger (" concore" )
1515logger .addHandler (logging .NullHandler ())
1616
17- #these lines mute the noisy library
18- logging .getLogger (' matplotlib' ).setLevel (logging .WARNING )
19- logging .getLogger (' PIL' ).setLevel (logging .WARNING )
20- logging .getLogger (' urllib3' ).setLevel (logging .WARNING )
21- logging .getLogger (' requests' ).setLevel (logging .WARNING )
17+ # these lines mute the noisy library
18+ logging .getLogger (" matplotlib" ).setLevel (logging .WARNING )
19+ logging .getLogger (" PIL" ).setLevel (logging .WARNING )
20+ logging .getLogger (" urllib3" ).setLevel (logging .WARNING )
21+ logging .getLogger (" requests" ).setLevel (logging .WARNING )
2222
2323
24- # if windows, create script to kill this process
24+ # if windows, create script to kill this process
2525# because batch files don't provide easy way to know pid of last command
2626# ignored for posix != windows, because "concorepid" is handled by script
2727# ignored for docker (linux != windows), because handled by docker stop
28- if hasattr (sys , ' getwindowsversion' ):
29- with open ("concorekill.bat" ,"w" ) as fpid :
30- fpid .write ("taskkill /F /PID " + str (os .getpid ())+ "\n " )
28+ if hasattr (sys , " getwindowsversion" ):
29+ with open ("concorekill.bat" , "w" ) as fpid :
30+ fpid .write ("taskkill /F /PID " + str (os .getpid ()) + "\n " )
3131
3232ZeroMQPort = concore_base .ZeroMQPort
3333convert_numpy_to_python = concore_base .convert_numpy_to_python
3838zmq_ports = {}
3939_cleanup_in_progress = False
4040
41- s = ''
42- olds = ''
41+ s = ""
42+ olds = ""
4343delay = 1
4444retrycount = 0
45- inpath = "./in" # must be rel path for local
45+ inpath = "./in" # must be rel path for local
4646outpath = "./out"
4747simtime = 0
4848
49+
4950def _port_path (base , port_num ):
5051 return base + str (port_num )
5152
53+
5254concore_params_file = os .path .join (_port_path (inpath , 1 ), "concore.params" )
5355concore_maxtime_file = os .path .join (_port_path (inpath , 1 ), "concore.maxtime" )
5456
@@ -58,16 +60,19 @@ def _port_path(base, port_num):
5860
5961_mod = sys .modules [__name__ ]
6062
63+
6164# ===================================================================
6265# ZeroMQ Communication Wrapper
6366# ===================================================================
6467def init_zmq_port (port_name , port_type , address , socket_type_str ):
6568 concore_base .init_zmq_port (_mod , port_name , port_type , address , socket_type_str )
6669
70+
6771def terminate_zmq ():
6872 """Clean up all ZMQ sockets and contexts before exit."""
6973 concore_base .terminate_zmq (_mod )
7074
75+
7176def signal_handler (sig , frame ):
7277 """Handle interrupt signals gracefully."""
7378 print (f"\n Received signal { sig } , shutting down gracefully..." )
@@ -78,20 +83,23 @@ def signal_handler(sig, frame):
7883 concore_base .terminate_zmq (_mod )
7984 sys .exit (0 )
8085
86+
8187# Register cleanup handlers
8288atexit .register (terminate_zmq )
83- signal .signal (signal .SIGINT , signal_handler ) # Handle Ctrl+C
84- if not hasattr (sys , ' getwindowsversion' ):
89+ signal .signal (signal .SIGINT , signal_handler ) # Handle Ctrl+C
90+ if not hasattr (sys , " getwindowsversion" ):
8591 signal .signal (signal .SIGTERM , signal_handler ) # Handle termination (Unix only)
8692
8793params = concore_base .load_params (concore_params_file )
8894
89- #9/30/22
95+
96+ # 9/30/22
9097def tryparam (n , i ):
9198 """Return parameter `n` from params dict, else default `i`."""
9299 return params .get (n , i )
93100
94- #9/12/21
101+
102+ # 9/12/21
95103# ===================================================================
96104# Simulation Time Handling
97105# ===================================================================
@@ -100,12 +108,15 @@ def default_maxtime(default):
100108 global maxtime
101109 maxtime = safe_literal_eval (concore_maxtime_file , default )
102110
111+
103112default_maxtime (100 )
104113
114+
105115def unchanged ():
106116 """Check if global string `s` is unchanged since last call."""
107117 return concore_base .unchanged (_mod )
108118
119+
109120# ===================================================================
110121# I/O Handling (File + ZMQ)
111122# ===================================================================
@@ -116,5 +127,6 @@ def read(port_identifier, name, initstr_val):
116127def write (port_identifier , name , val , delta = 0 ):
117128 concore_base .write (_mod , port_identifier , name , val , delta )
118129
119- def initval (simtime_val_str ):
130+
131+ def initval (simtime_val_str ):
120132 return concore_base .initval (_mod , simtime_val_str )
0 commit comments