Python 时间循环
我是python编程新手。我有个问题。我想每15分钟保存一次输入数据(要列出的原始数据)。15分钟后,列表将被删除,并再次写入输入数据。有人能帮我吗?谢谢你的好意Python 时间循环,python,Python,我是python编程新手。我有个问题。我想每15分钟保存一次输入数据(要列出的原始数据)。15分钟后,列表将被删除,并再次写入输入数据。有人能帮我吗?谢谢你的好意 from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory def WriteListtoCSV (data): with open ('tesdata.csv','a') as csvfile:
from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
def WriteListtoCSV (data):
with open ('tesdata.csv','a') as csvfile:
writer=csv.writer(csvfile)
for val in data:
writer.writerow([val])
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
# echo back message verbatim
self.sendMessage(payload, isBinary)
mins = 0
data_nilai = [ ]
while mins != 60: #change value with seconds
data_nilai.append(payload.decode('utf8'))
time.sleep(1)
mins+=1
WriteListtoCSV(data_nilai)
#ClearCSV()
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
import sys
import csv
import time
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
factory = WebSocketServerFactory(u"ws://192.168.1.23:9000", debug=False)
factory.protocol = MyServerProtocol
# factory.setProtocolOptions(maxConnections=2)
reactor.listenTCP(9000, factory)
reactor.run()
我只关注消息下面是带有小代码的Algo 算法:
import pickle
import time
import os
detail_file = "/tmp/test.txt"
while(1):
# Get input from User and split to List.
user_input = raw_input("Enter item of the list separated by comma:")
user_input = user_input.split(",")
print "User List:- ", user_input
#- Save process, We can save your data i.e. list into file or database or any where
with open(detail_file, "wb") as fp:
pickle.dump(user_input, fp)
# Wait for 15 minutes.
time.sleep(900) # 15 * 60 = 900
# delete Save details.
os.remove(detail_file)
import pickle
import time
import os
detail_file = "/tmp/test.txt"
while(1):
# Get input from User and split to List.
user_input = raw_input("Enter item of the list separated by comma:")
user_input = user_input.split(",")
print "User List:- ", user_input
#- Save process, We can save your data i.e. list into file or database or any where
with open(detail_file, "wb") as fp:
pickle.dump(user_input, fp)
# Wait for 15 minutes.
time.sleep(900) # 15 * 60 = 900
# delete Save details.
os.remove(detail_file)
注意:
import pickle
import time
import os
detail_file = "/tmp/test.txt"
while(1):
# Get input from User and split to List.
user_input = raw_input("Enter item of the list separated by comma:")
user_input = user_input.split(",")
print "User List:- ", user_input
#- Save process, We can save your data i.e. list into file or database or any where
with open(detail_file, "wb") as fp:
pickle.dump(user_input, fp)
# Wait for 15 minutes.
time.sleep(900) # 15 * 60 = 900
# delete Save details.
os.remove(detail_file)
使用input()
获取Python 3.x的用户信息
使用raw\u input()
获取Python 2.x的用户信息
[编辑1]
Crontab
参考:
参考:
OS:CentOS
要编辑crontab,请使用以下命令:
crontab -e
*/15 * * * * python /tmp/script.py
其中crontab条目结构为:
m h dom mon dow command
欢迎来到SO。在这里,我们可以尝试修复您的代码,如果它不工作,但我们不会为您编写代码。您必须先尝试编写代码,然后我们才能帮助您。请发布一些试用代码,我们可以一起向前推进:)
raw\u input
,time
和在python中使用列表
可能是您的起点。在问题中发布该代码。这可能会有更多帮助:)您还打算对该数据做什么?谢谢。我是说。我只在时间超过15分钟的情况下发送数据,不提供15分钟的延迟。然后需要设置cron-tab
以始终运行发送脚本或运行py-file,并在每15分钟后调用您的脚本。您能否使用cron-tab
给出示例请在回答中添加crontab详细信息,您可以检查此项,如果需要更多详细信息,请告诉我