gpt4 book ai didi

python - 守护程序输出未写入 TXT

转载 作者:太空狗 更新时间:2023-10-29 11:18:02 27 4
gpt4 key购买 nike

我有一个从守护程序服务器调用函数的脚本。守护进程服务器每次完成其工作时都会生成一个输出。守护进程主要从外部服务器下载文件。守护程序源代码已关闭(由外部公司编码)。

完成文件下载后的守护程序输出示例:

Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

我正在使用的脚本从一个 TXT 文件中获取下载的 ID,从用户字段中获取 PC 的用户名,并将它们放入一个数组中,然后从守护进程中调用它们获取文件,然后在txt文件。

日志样本应该是:

Documnent ID= 3
From User = DomainCon
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

我面临的问题是,保存到/home/ubuntu/Daemon/downloads/download_1024000003.pdf 守护程序服务器每次下载完成时生成的 TXT 有时会丢失。 p>

问题示例:

Documnent ID= 2
From User = DomainCon
Documnent ID= 3
In Chat = DomainCon
ANSWER 78
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

日志文件中的输出应该是:

Documnent ID= 2
From User = DomainCon
ANSWER 96
Saved to /home/ubuntu/Daemon/downloads/download_1024000002.pdf
Documnent ID= 3
In Chat = DomainCon
ANSWER 78
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

这是我正在使用的脚本,包括注释:

for i in range(len(ids)):\\ ids is the array that contains the documents that should be downloaded.
cmdping = "sleep 5; echo load_document "+ids[i][0]+"| nc -w 4 127.0.0.1 1234 | tee >> "+logtxt \\ Perpare the Echo command to the daemon to start the download tee to save the output of the daemon into the log text
print("\033[1;31m~~~Documnent~~~NB~~~"+str(i+1)+"\033[1;m")
print ("Documnent ID= "+ids[i][0])\\ Just Print on screen for Debbuging Purpose
print ("From User= "+ids[i][1])\\Just Print on screen for Debbuging Purpose
logfile = open(logtxt, "a") \\ Open the TXT where the log is gonna be saved.
logfile.write("Documnent ID= "+ids[i][0]+"\n")\\Write the Document ID in the File.
logfile.write("From User = "+ids[i][1]+"\n")\\\\Write the From User in the File.
logfile.close()\\Close the logfile
if (i==len(ids)-1):
p=subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)\\ Start the proccess that was prepared before.
#time.sleep(1)
check=""
s=1
skip=0
while (s==1):
check=checkfile(srcdir)
if (check!="no"):
s=2
if(check=="skip"):
skip=1
if(skip!=1):
if(checksize(srcdir+check) == "done"):
print(check+"\n \033[1;32mFinished Downloading moving to the next download\033[1;m")
p.terminate()
user_dir=rootdir+"/document/"+ids[i][1];
checkandcopy(download,user_dir)

最佳答案

您可以考虑改用套接字。这里有一些灵感:

import re
import socket
for i, (doc_id, user) in enumerate(ids):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("\033[1;31m~~~Documnent~~~NB~~~"+str(i+1)+"\033[1;m")
s.connect(('127.0.0.1', 1234))
s.sendall('load_document {}\n'.format(doc_id))
buf = []
while True:
r = s.recv(4096).strip()
print('Got {} for socket'.format(r))
buf.append(r)
if r.endswith('.pdf'):
print('Done')
break
result = '\n'.join(buf)
s.close()
with open(logtxt, 'a') as f:
f.write('Document ID= {}\n'.format(doc_id))
f.write('From User = {}\n'.format(user))
f.write('{}\n'.format(result))
filename = re.search('Saved to (.+)$', result).group(1)
checkandcopy(filename, userdir)

关于python - 守护程序输出未写入 TXT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35310784/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com