Reputation: 2663
I am importing Schedule
into Flask
. My project contains WSGI
however I know little about the relationship between Flask
and WSGI
. Now I have three main files:
wsgi.py
: Automatically generated by other tool.app.py
: I put client request here.test.py
: Used to test Schedule
.I want to start a task which is a long task when server launch. Here is the part of wsgi.py
:
# -*- coding: utf-8 -*-
from threading import Thread
import test
t = Thread(target=test.job)
t.start()
if __name__ == '__main__':
...
As you see I start a thread and let the job work in it.Here is my test.py
.
import schedule
def job():
schedule.every(1).seconds.do(pr)
def pr():
print("I'm working...")
My problem is that the job
never starts.
Upvotes: 2
Views: 1820
Reputation: 2663
I find out my problem.I never let schedule execute jobs. Now wsgi.py
looks like this.
# -*- coding: utf-8 -*-
from threading import Thread
import test
schedule.every(1).seconds.do(test.job)
t = Thread(target=test.run_schedule)
t.start()
if __name__ == '__main__':
...
And test.py
:
import schedule
import time
start_time = time.time()
def job():
print("I'm working..." + str(time.time() - start_time))
def run_schedule():
while True:
schedule.run_pending()
time.sleep(1)
In order to work in separate thread, I create a thread and in this thread I loop every 1ms. In loop, schedule
invoke run_pending
to call the job
if time out (in my case it's 1s).
Upvotes: 1