Ad

Python Scheduler Not Running A Function Scheduled In An Import

- 1 answer

Problem

When this block of code:

# main.py - this file gets run directly.
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)

print("before definition")

def do_something():
    print("do_something is being executed.")
    scheduler.enter(1, 5, do_something)

print("post definition")
scheduler.enter(1, 5, do_something)

# Do something, so that the program does not terminate.
do_not_terminate()

is in the file being invoked (main.py) it runs as expected(with the do_something function being run every second.), producing the following output:

before definition
post definition
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
do_something is being executed.
...

But:

when the above block is put into a separate file (someimport.py) and someimport.py gets imported in main.py the do_something function no longer gets executed. [The code looks like this:]

# someimport.py - this file gets imported in main.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)

print("before definition")

def do_something():
    print("do_something is being executed.")
    scheduler.enter(1, 5, do_something)

print("post definition")
scheduler.enter(1, 5, do_something)

# main.py - this file get's run directly
import someimport

# Do something, so that the program does not terminate.
do_not_terminate()

Only the following output is produced (and of course no error messages appear):

before definition
post definition

I already tried the following:

1. Switch import someimport to from someimport import * in main.py

This, as somewhat expected, did not change anything.

2. Put the first scheduler.enter call into a separate function(run), which is called after the import:
# someimport.py - this file gets imported in main.py
import sched
import time
scheduler = sched.scheduler(time.time, time.sleep)

print("before definition")

def do_something():
    print("do_something is being executed.")
    scheduler.enter(1, 5, do_something)

print("post definition")
def run():
    scheduler.enter(1, 5, do_something)

# main.py - this file get's run directly
import someimport

someimport.run()

# Do something, so that the program does not terminate.
do_not_terminate()

This did not change anything.

3. importing sched and time in main.py aswell.

As expected, this also did not have any effect.

Is there any way to schedule the do_something function in someimport.py, without having the first scheduler.enter in the main.py file. That is the thing I'm trying to avoid (as in the real project there are ~100 scheduled tasks like this and I'm trying to clean up the main file).

The all the code above has been tested on python 3.7.3 under GNU/Linux.

Many thanks in advance!

Ad

Answer

I was able to get your separate module example working only by calling run() on the scheduler instance after importing it into main.py.

# main.py - this file get's run directly
import someimport

someimport.scheduler.run()  # Blocks until all events finished

Produces:

before definition
post definition
do_something is being executed.
do_something is being executed.

According to the docs, scheduler.run() has to be called in order to run any scheduled tasks. The scheduler.enter() method on it's own will just schedule events, not run them.

Ad
source: stackoverflow.com
Ad