首页 > 解决方案 > 如何在 apscheduler 中使用函数的返回值

问题描述

我是 Python 调度程序的新手。我有来自两个函数 'tick & 'tock' 的返回值(使用变量 Out1 和 Out2)。Out1 和 Out2 在这两个函数内分别递增。我想在第三个函数“时钟”中使用 Out1 和 Out2 的值。请参阅下面的代码:

from datetime import datetime
import os

from apscheduler.schedulers.blocking import BlockingScheduler

Out1 = 0
Out2 = 1
print('Out1 = {}; Out2 = {}'.format(Out1, Out2))

def tick(Msg):
    print('Tick:  {} {}'.format(datetime.now(), Msg))
    global Out1
    Out1 += 1
    return Out1

def tock(Msg):
    print(' Tock: {} {}'.format(datetime.now(), Msg))
    global Out2
    Out2 += 2
    return Out2

def Clock(Out1, Out2):
    print('\tClock: {} {} {}'.format(datetime.now(), Out1, Out2))
    return 

if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(tick, 'interval', seconds=1,args=["Hello"])
    scheduler.add_job(tock, 'interval', seconds=5,args=["Bye.."])
    scheduler.add_job(Clock, 'interval', seconds=2,args=[tick, tock])
#   scheduler.add_job(Clock, 'interval', seconds=2,args=[tick("HELLO"), tock("BYE")])
    print('Press Ctrl+C to exit')

    try:
        scheduler.start()

    except (KeyboardInterrupt, SystemExit):
        pass
print('\t You made me quit !')

我得到的输出是:

Out1 = 0; Out2 = 1
Press Ctrl+C to exit
Tick:  2019-08-18 17:38:21.262307 Hello
Tick:  2019-08-18 17:38:22.263210 Hello
    Clock: 2019-08-18 17:38:22.263978 <function tick at 0x7f299de9dea0> <function tock at 0x7f299c0d66a8>
Tick:  2019-08-18 17:38:23.263754 Hello
Tick:  2019-08-18 17:38:24.263164 Hello
    Clock: 2019-08-18 17:38:24.263934 <function tick at 0x7f299de9dea0> <function tock at 0x7f299c0d66a8>
Tick:  2019-08-18 17:38:25.263955 Hello
 Tock: 2019-08-18 17:38:25.264638 Bye..
Tick:  2019-08-18 17:38:26.263757 Hello
    Clock: 2019-08-18 17:38:26.264502 <function tick at 0x7f299de9dea0> <function tock at 0x7f299c0d66a8>
Tick:  2019-08-18 17:38:27.263805 Hello

我如何获得输出,例如:

Out1 = 0; Out2 = 1
Press Ctrl+C to exit
Tick:  2019-08-18 17:38:21.262307 Hello
Tick:  2019-08-18 17:38:22.263210 Hello
    Clock: 2019-08-18 17:38:22.263978 2 1
Tick:  2019-08-18 17:38:23.263754 Hello
Tick:  2019-08-18 17:38:24.263164 Hello
    Clock: 2019-08-18 17:38:24.263934 4 1
Tick:  2019-08-18 17:38:25.263955 Hello
 Tock: 2019-08-18 17:38:25.264638 Bye..
Tick:  2019-08-18 17:38:26.263757 Hello
    Clock: 2019-08-18 17:38:26.264502 6 3
Tick:  2019-08-18 17:38:27.263805 Hello

我想要做的是,使用时钟中的 Out1 和 Out2 的值。为什么我使用 global,因为我不知道更好:)

标签: python-3.x

解决方案


感谢 Carcigenicate 提出这些问题。它让我进一步思考并解决了我的问题。我没有想,我想。修复后答案很简单!它是为像我这样的新手发布的...... :)

from datetime import datetime
import os

from apscheduler.schedulers.blocking import BlockingScheduler

Out1 = 0
Out2 = 1
print('Out1 = {}; Out2 = {}'.format(Out1, Out2))

def tick(Msg):
    global Out1
    print('Tick:  {} {} {} {}'.format(datetime.now(), Msg, Out1, Out2))
    Out1 += 1
    return

def tock(Msg):
    global Out2
    print(' Tock: {} {} {} {}'.format(datetime.now(), Msg, Out1, Out2))
    Out2 += 2
    return

def Clock():
    print('\tClock: {} {} {}'.format(datetime.now(), Out1, Out2))
    return 

if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(tick, 'interval', seconds=1,args=["Hello"])
    scheduler.add_job(tock, 'interval', seconds=5,args=["Bye.."])
    scheduler.add_job(Clock, 'interval', seconds=2)

    print('Press Ctrl+C to exit')

    try:
        scheduler.start()

    except (KeyboardInterrupt, SystemExit):
        pass
print('\t You made me quit !')

推荐阅读