在 Python 中使用 Schedule 进行多处理

Multiprocessing with Schedule in Python

我正在尝试结合这两个代码。我想在两个 不同的循环 中达到 运行。

例如如果我没有在预定的时间写条目,它必须打印"Good Luck for Test"。我想让定时任务独立行动

import schedule 

import time 

def good_luck(): 

    print("Good Luck for Test") 

schedule.every().day.at("00:00").do(good_luck)

while True: 

    schedule.run_pending() 

    time.sleep(1) 


def assistant():

    command = input('input: ')

    if command == '1':

        print('is it one')

    else:

        print('is not one')    

while True:

    assistant()

示例输出


Good Luck for Test #automatically at specified times
input: 1
is it one
input: 2
is not one
Good Luck for Test #automatically at specified times
Good Luck for Test #automatically at specified times
Good Luck for Test #automatically at specified times

etc.

multiprocessing Python 模块可能有效。但是,您可能需要修改输入方法才能获得预期的结果。

import schedule 
import time 
import multiprocessing

def good_luck():
#    schedule.every().day.at("00:00").do(good_luck)
    schedule.every(1).minutes.do(_good_luck)
    while True: 
        schedule.run_pending() 
        time.sleep(1) 

def _good_luck(): 
    print("Good Luck for Test") 


def assistant():
    while True: 
        command = input('input: ')
        if command == '1':
            print('is it one')
        elif command.lower() == 'quit':
            return
        else:
            print('is not one')    

if __name__ == '__main__':
    jobs = []
    p1 = multiprocessing.Process(target=good_luck)
    jobs.append(p1)
    p1.start()
    assistant()