我开发了一个机器学习python脚本(让我们称之为用python3.6编写的classify_obj )来导入TensorFlow。它最初是为批量分析而开发的,但现在我发现需要在较小的数据集中反复运行此脚本,以满足更多的实时使用。我是在Linux RH7上这样做的。
工艺流程:
categorize.
G 211
重复#1为了分解所花费的时间,我关闭了主逻辑,只进行模块导入而不执行任何其他操作。我发现导入在小数据集中的7-10秒运行时中占用了大约4到5s。分类时间约为2s。我也在考虑其他方法,以减少其他领域的运行时间,但大部分似乎来自进口。
进口时间: 4-6s
分类时间: 1s
读、写和其他逻辑时间: 0.2s
我在想有什么办法可以缩短进口时间呢?
我的一个想法是将classify_obj修改为一个“保持活着”的过程。主工具在完成其所有活动后将停止此流程/服务。其意图(不确定是否会是这样)是,所有所需的库都已在流程启动期间加载,并且当主工具调用该流程/服务时,它只会导致分类时间,而不需要导入重复的库。
你觉得这个怎么样?另外,我如何在LinuxRHEL7.4上设置它呢?请提供一些参考链接。
其他建议将不胜感激。
谢谢,祝您今天愉快!
发布于 2020-03-24 08:18:42
这是我为实现上述目标而设计的解决方案。
参考资料:https://realpython.com/python-sockets/
我必须创建两个脚本。1.客户端python脚本:用于使用套接字编程将要分类的原始数据传递给服务器python脚本。
最后,将分类时间缩短到0.1 ~ 0.3s。
客户端脚本
import socket
import argparse
from argparse import ArgumentParser
def main():
parser = ArgumentParser(description='XXXXX')
parser.add_argument('-i','--input', default='NA', help='Input txt file path')
parser.add_argument('-o','--output', default='NA', help='Output csv path with class')
parser.add_argument('-stop','--stop', default='no', help='Stop the server script')
args = parser.parse_args()
str = args.input + ',' + args.output + ',' + args.stop
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
bytedata = str.encode()
sock.send(bytedata)
data = sock.recv(1024)
print('Received', data)
if __name__== "__main__":
main()服务器脚本
def main():
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST,PORT))
sock.listen(5)
stop_process = 'no'
while (stop_process == 'no'):
# print('Waiting for connection')
conn, addr = sock.accept()
data = ''
try:
# print('Connected by', addr)
while True:
data = conn.recv(1024)
if data:
stop_process = process_input(data) # process_input function processes incoming data. If client sends 'yes' for the stop argument, the stop_process variable will be set to 'yes' by the function.
byte_reply = stop_process.encode()
conn.sendall(byte_reply) # send reply back to client
else:
break
conn.close()
# print('Closing connection',addr)
finally:
conn.close()
if __name__== "__main__":
main()https://stackoverflow.com/questions/60521877
复制相似问题