嵌套函数不是在顶级定义的函数,因此这就是您收到错误的原因。您需要重新定位计算的定义
function1以及
function2计算之外。
编写方式时,您的流程将立即启动,而不是在安排它们运行的日期开始。那可能会达到您的预期目的:
import osimport timeimport randomfrom multiprocessing import Process, Queuefrom threading import Threadfrom datetime import datetimeimport scheduledef function1(q): while True: daydate = datetime.now() number = random.randrange(1, 215) fmt = '(pid: {}) Sent to function2: ({}, {})' print(fmt.format(os.getpid(), daydate, number)) q.put((daydate, number)) time.sleep(2)def function2(q): while True: date, number = q.get() fmt = "(pid: {}) Received values from function1: ({}, {})" print(fmt.format(os.getpid(), date, number)) # time.sleep(2) no need to sleep here because q.get will block until # new items are availabledef computation(): q = Queue() a = Process(target=function1, args=(q,)) a.start() b = Process(target=function2, args=(q,)) b.start() a.join() b.join()if __name__ == "__main__": # We are spawning new threads as a launching platform for # computation. Without it, the next job couldn't start before the last # one has finished. If your jobs always end before the next one should # start, you don't need this construct and you can just pass # ...do(computation) schedule.every().friday.at("01:02").do( Thread(target=computation).start ) schedule.every().friday.at("01:03").do( Thread(target=computation).start ) while True: schedule.run_pending() time.sleep(1)就像现在一样,一旦启动,您的进程将永远运行。如果这不是您想要的,则必须考虑实现一些停止条件。



