可以通過另開一條執行緒, 去專門做這件事情, py2**如下, 如果是py3請自行調整下語法
# coding: utf8
import threading
import time
# 真正要執行的函式
def t1():
print ('ok')
# 每隔10秒鐘執行
def t2():
while 1:
t1()
time.sleep(10)
if __name__ == '__main__':
t = threading.thread(target=t2)
t.start()
# 此處寫你主線程要處理的事情.....
t.join()
如果直接開子程序的話,退出主程序時子程序會一直存在, 建議設定成守護程序
import sys
import signal
import threading
import time
from datetime import datetime
def quit(signum, frame):
sys.exit()
def process_fun():
while true:
print datetime.now()
time.sleep(1)
if __name__ == '__main__':
try:
signal.signal(signal.sigint, quit)
signal.signal(signal.sigterm, quit)
p = threading.thread(target=process_fun)
#註冊成為主程序
p.setdaemon(true)
p.start()
#如果沒有主程序, 就用迴圈**
while true:
pass
except exception as e:
pass
shell 併發多程序同時執行
shell 併發多程序同時執行 複製 1 bin bash 2 3 send thread num 13 設定程序數。4 tmp fifofile tmp fifo 指令碼執行的當前程序id號作為檔名 5 mkfifo tmp fifofile 新建乙個隨機fifo管道檔案 6 exec 6 tmp...
shell 併發多程序同時執行
1 bin bash 23 send thread num 13 設定程序數。4 tmp fifofile tmp fifo 指令碼執行的當前程序id號作為檔名 5mkfifo tmp fifofile 新建乙個隨機fifo管道檔案 6 exec 6 tmp fifofile 定義檔案描述符6指向這...
python定時執行 每天
以下 實現了python的每天定時執行 import datetime import time import pymysql def dosth print test conn pymysql.connect host 192.0.9.169 port 5507,user writer passwd...