def test_has_label(self): def myfunc(): print('Hi') taskinfo = booker.do(myfunc, 'in 10 seconds', 'my-label') assert taskinfo.task.label == 'my-label' booker.cancel_all()
def test_do_return_types(self): def myfunc(): print('Hi') taskinfo = booker.do(myfunc, 'in 10 seconds') assert isinstance(taskinfo, booker.SingleTask) assert isinstance(taskinfo.task, booker.Task) assert isinstance(taskinfo.schedule, booker.Schedule) taskinfo = booker.do(myfunc, 'every 10 seconds in 10 seconds') assert isinstance(taskinfo, booker.RepeatingTask) assert isinstance(taskinfo.task, booker.Task) assert isinstance(taskinfo.schedule, booker.Schedule) taskinfo = booker.do(myfunc, 'at 12:00') assert isinstance(taskinfo, booker.SingleTask) taskinfo = booker.do( myfunc, 'every 5 seconds in 15 minutes until 01-30-2020 12:00') assert isinstance(taskinfo, booker.RepeatingTask) booker.cancel_all()
import sys import time import booker def myfunc(): print('Starting') time.sleep(2) print('Done') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') booker.do(myfunc, 'in 0 seconds') while True: try: time.sleep(1) except KeyboardInterrupt: sys.exit(0)
def test_cancel(self): def myfunc(): print('Hi') # In case we created other tasks in other tests booker.cancel_all() assert len(booker.tasks()) == 0 # Cancelling by label. booker.do(myfunc, 'in 1 second', 'dummy-task') booker.do(myfunc, 'in 1 second', 'dummy-task') booker.do(myfunc, 'in 1 second', 'dummy-task') booker.do(myfunc, 'in 1 second', 'dummy-task') assert len(booker.tasks()) == 4 booker.cancel('dummy-task') assert len(booker.tasks()) == 0 # Using .cancel_all(). booker.do(myfunc, 'in 1 second') booker.do(myfunc, 'in 1 second') assert len(booker.tasks()) == 2 booker.cancel_all() assert len(booker.tasks()) == 0
def in_ten_seconds(): print('- It\'s been 10 seconds.') print(booker.elapsed_since_epoch()) # --- # Using the decorator, with a label. @booker.task('every 1 second', 'my-ping-task') def ping(): print('- Ping') # --- # Using .do(). def pong(): print('- Pong') booker.do(pong, 'every 1 second in 2 seconds', 'my-pong-task') def print_task_status(): print('- Printing the status of all tasks:') for task in booker.tasks(): print(task) booker.do(print_task_status, 'in 3 seconds') # --- # Using .do(). def cancel_by_label(label): print('- Cancelling tasks with label: \'{}\'...'.format(label)) booker.cancel(label) booker.do(lambda: cancel_by_label('my-ping-task'), 'in 12 seconds')