我的脚本执行以下操作:
我使用了一个const mapStateToProps = (state) => {
return {
router: state.router,
location: state.route.location
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Header)
类,该类继承了 <NavLink
exact
to="/logs"
className="menu-item"
activeClassName="selected"
>
<i className="st-icon-log-reports" /> logs
</NavLink>
流中的内容,以提供非阻塞实现
我创建了两个进程以执行ReaderThread
threading.Thread
我创建了一个actisense -r /dev/ttyUSB0 | analyzer -json
,如下所示:
actisense_process = Popen(['actisense-serial', '-r', '/dev/ttyUSB0'],stdout=PIPE)
analyzer_process = Popen(['analyzer', '-json'],stdin=actisense_process.stdout, stdout=PIPE, stderr=PIPE)
创建了一个ReaderThread
函数:
class ReaderThread(threading.Thread):
def __init__(self, stream):
threading.Thread.__init__(self)
self.stream = stream
def run(self):
while True:
line = self.stream.readline().decode('utf-8')
if len(line) == 0:
break
try:
n2k_dict = json.loads(line)
print(n2k_dict)
except Exception as e:
raise(e)
当前,当按下 CTRL C 时,脚本确实关闭,但是我希望退出脚本更加安全,因为该脚本需要在嵌入式Single Board计算机上运行。
我想知道这里是否需要遵循特定的顺序。例如。首先关闭main
,然后关闭def main():
reader = ReaderThread(analyzer_process.stdout)
try:
reader.start()
analyzer_process.wait()
reader.join()
except Exception as e:
# usually if user presses `CTRL+C` it should safely be exited
reader.close()
os.kill(os.getpid(analyzer_process.pid), signal.SIGTERM)
os.kill(os.getpid(actisense_process.pid), signal.SIGTERM)
,反之亦然,然后关闭一个或多个线程,然后再关闭两个进程。