我想要一个函数在主程序仍在运行时每 5 分钟运行一次。
我发现了多个关于如何使函数每隔几秒运行一次的帖子,但它们似乎对我不起作用。
这是我的程序:
from Read import getUser, getMessage
from Socket import openSocket, sendMessage
from Initialize import joinRoom, Console
from question import query_yes_no
from Settings import AIDENT
import string
import sched, time
import urllib.parse
import requests
import subprocess
import sys
import os
s = openSocket()
joinRoom(s)
while True:
try:
try:
readbuffer = s.recv(1024)
readbuffer = readbuffer.decode()
temp = readbuffer.split("\n")
readbuffer = readbuffer.encode()
readbuffer = temp.pop()
except:
temp = ""
for line in temp:
if line == "":
break
if "PING" in line and Console(line):
msgg = (("PONG tmi.twitch.tv\r\n").encode())
print(msgg)
s.send(msgg)
break
user = getUser(line)
message = getMessage(line)
print (user + " > " + message)
PMSG = "/w " + user + " "
if "!ping" in message:
sendMessage(s, "PONG ( i'm working fine )")
except:
pass
Run Code Online (Sandbox Code Playgroud)
我需要每 5 分钟运行一次 sendMessage() 函数而不中断主程序。
您必须使用线程,以防您的 main 方法将继续在单独的线程中执行,并且转发器函数将在每 nth 秒后执行示例代码如下所示:
import threading
def printit():
threading.Timer(5.0, printit).start()
print "Hello, World!"
printit()
Run Code Online (Sandbox Code Playgroud)
自己尝试一下吧。
| 归档时间: |
|
| 查看次数: |
7722 次 |
| 最近记录: |