3

I have a class that has triggers, and I'd like to update these triggers when the job hit the proper time.

My class:

class SetTrigger(object):
    def __init__(self):
        self.set_work = False
        self.set_reload = False
        self.set_refresh = False
    
    def UpdateSetRefresh(self):
        self.set_refresh = True
        return self.set_refresh

    def UpdateSetWork(self):
        self.set_work = True
        return self.set_work

    def UpdateSetReload(self):
        self.set_reload = True
        return self.set_reload

If I set the variable to get the value from the function as below I get one error.

trigger = SetTrigger()
set_refresh = trigger.UpdateSetRefresh()

Error:

func must be a callable or a textual reference to one

I already tried to call trigger.UpdateSetRefresh but the variable set_refresh doesn't change to True. If I call trigger.UpdateSetRefresh() it gives me the same error above.

How can I call the function UpdateSetRefresh from class SetTrigger in APScheduler job as below?

I tried:

scheduler.add_job(trigger.UpdateSetRefresh, 'interval', seconds=20, id='1', name='refresh_hereoes_positions', misfire_grace_time=180)

[EDIT]

Example that trigger.UpdateSetRefresh is not returning True as I need:

import tzlocal
import time
import os
from bot import SetTrigger
from apscheduler.schedulers.asyncio import AsyncIOScheduler

trigger = SetTrigger()

scheduler = AsyncIOScheduler(timezone=str(tzlocal.get_localzone()))
scheduler.add_job(trigger.UpdateSetRefresh, 'interval', seconds=3)
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

try:
    # This is here to simulate application activity (which keeps the main thread alive).
    while True:
        time.sleep(2)
        print(trigger.set_refresh)
except (KeyboardInterrupt, SystemExit):
    # Not strictly necessary if daemonic mode is enabled but should be done if possible
    scheduler.shutdown()

Output:

Press Ctrl+Break to exit
False
False
False
Guilherme Matheus
  • 573
  • 10
  • 30

1 Answers1

1

You have to pass function to add_job but instead of it you passed returned value of the trigger.UpdateSetRefresh.
So correct statement would be like:

scheduler.add_job(trigger.UpdateSetRefresh, 'interval', seconds=2, id='1', name='refresh_hereoes_positions')

instead of

scheduler.add_job(trigger.UpdateSetRefresh(), 'interval', seconds=2, id='1', name='refresh_hereoes_positions')

When you add parentheses at the end of the function name python will execute it and pass returned value to the add_job function.
For more information about passing function as argument check out this link.

[EDIT]
If you are using AsyncIOScheduler you should do something like this

import tzlocal
import os
from bot import SetTrigger
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import asyncio


def DisplayValue(trigger):
    print(trigger.UpdateSetWork())


trigger = SetTrigger()
scheduler = AsyncIOScheduler(timezone=str(tzlocal.get_localzone()))
scheduler.add_job(trigger.UpdateSetRefresh, 'interval', seconds=3)
scheduler.start()
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))


try:
    loop = asyncio.get_event_loop()
    loop.call_at(10, displayValue, trigger)
    loop.run_forever()
except (KeyboardInterrupt, SystemExit):
    scheduler.shutdown()

Or you can use Background scheduler like that

from apscheduler.schedulers.background import BackgroundScheduler
from bot import SetTrigger
import tzlocal
import time

trigger = SetTrigger()
scheduler = BackgroundScheduler()
scheduler.configure(timezone=str(tzlocal.get_localzone()))
scheduler.add_job(trigger.UpdateSetRefresh, 'interval', seconds=2, id='1', name='refresh_hereoes_positions')
scheduler.start()

while True:
    print(trigger.set_refresh)
    time.sleep(2)