消息关闭
    暂无新消息!

如题,求教!


4个回答

︿ 1

如果直接开子进程的话,退出主进程时子进程会一直存在, 建议设置成守护进程

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
︿ 0

可以通过另开一条线程, 去专门做这件事情, 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()
︿ 0

threading.Timer

import threading as thd
import time

def fn():
    print(time.time())
    thd.Timer(10,fn).start()
    
fn()