Skip to content Skip to sidebar Skip to footer

Passing Arguments When Scheduling A Function With Asyncio/aiocron

I am using the aiocron library to schedule function calls for certain times of day. This is a minimal example of what I want to do. In this example, I want to send a 'good morning'

Solution 1:

I solved this myself for now by using a closure:

class GoodMorningSayer:
    def __init__(self):
        @aiocron.crontab("0 8 * * *")
        @asyncio.coroutine
        def on_morning()
            yield from self.send_message("good morning!")       

    @asyncio.coroutine
    def send_message(text):
        #insert fancy networking stuff here        

In the actual program I wrapped this in some boilerplate that eventually calls self.say_good_morning to keep the code cleaner


Post a Comment for "Passing Arguments When Scheduling A Function With Asyncio/aiocron"