python3.x - How to implement python to run a specified function every 10 seconds? The waiting process cannot interrupt the main thread!
扔个三星炸死你
扔个三星炸死你 2017-06-12 09:22:32
0
7
1894

Like the title, please give me some advice!

扔个三星炸死你
扔个三星炸死你

reply all(7)
小葫芦

You can open another thread to do this specifically. The py2 code is as follows. If it is py3, please adjust the syntax yourself

# 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()
某草草

threading.Timer

import threading as thd
import time

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

If you start a child process directly, the child process will always exist when you exit the main process. It is recommended to set it as a daemon process

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
左手右手慢动作

You can consider Advanced Python Scheduler (http://apscheduler.readthedoc...
It can carry out extremely complex timing design, every few seconds, minutes, or a specific moment of a certain day, etc., it can block the process, and it can be in the background , all according to your requirements

.
左手右手慢动作
# -*- coding: utf-8 -*-
import gevent
import time


class Timer(object):
    """定时器,定时执行指定的函数
    """

    def __init__(self, start, interval):
        """
        @start, int, 延迟执行的秒数
        @interval, int, 每次执行的间隔秒数
        """
        self.start = start
        self.interval = interval

    def run(self, func, *args, **kwargs):
        """运行定时器
        :param func: callable, 要执行的函数
        """
        time.sleep(self.start)
        while True:
            func(*args, **kwargs)
            time.sleep(self.interval)


def send_message():
    pass


if __name__ == "__main__":
    scheduler = Timer(5, 10 * 60)
    gevent.spawn(scheduler.run(send_message))
typecho

Python task scheduling module – APScheduler (click to view)

APScheduler is a Python scheduled task framework, which is very convenient to use. It provides tasks based on date, fixed time interval and crontab type, and can persist tasks and run applications in daemon mode.

The following is a simple example, printing hello world every 10 seconds

from apscheduler.schedulers.blocking import BlockingScheduler
def my_job():
    print 'hello world'
 
sched = BlockingScheduler()
sched.add_job(my_job, 'interval', seconds=10)
sched.start()
Peter_Zhu
#-*- coding:utf8 -*- 

import multiprocessing
import time

def f():
    print time.ctime(),'这是子进程,每10S执行一次'

def work():
   while 1:
       f()
       time.sleep(10)

if __name__ == '__main__':
    p = multiprocessing.Process(target=work,)
    p.start()
    p.deamon = True
    while 1:
        print '这是主进程,每1秒执行一次'
        time.sleep(1)

Execution result:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template