miloira / schedule-decorator

A wonderful decorator package from schedule.
GNU Affero General Public License v3.0
4 stars 0 forks source link

Not respecting the set time #1

Open NannoSilver opened 2 years ago

NannoSilver commented 2 years ago

Hi,

This is an interesting package and perfect for my application. I did a short test and found that, unfortunately, it is not working as expected.

This is my code:

from schedule_decorator import cron

x = 0
@cron.every('1','seconds')
def task():
    global x
    x = x + 1
    print(x)

@cron.every('5','seconds')
def task():
    print('------------')

cron.run()

I was expecting to see the counter increasing by 1 every second, and a dashed line every 5 seconds. But I got a different result, with the counter printing twice per second, and the dashed line showing in incorrect position, also twice.

This is the output of above code:

1
2
3
4
5
6
7
8
------------
------------
9
10
11
12
13
14
15
16
17
18
------------
------------
19
20

I hope that can be fixed.

miloira commented 2 years ago

Thank you for your feedback, I published new version that fixs the bug you describe, upgrade your local package to version 2.1.2. @sukhoi47

NannoSilver commented 2 years ago

Thank you for so fast fixing! I am impressed with your professionalism.

Now it works fine in my local (Windows) computer as part of a Flask app, however, the same app at the Litespeed server it does not works. My app does not start. No error is logged. If I remove the schedule-decorator, then my app works fine.

I guess schedule-decorator is not compatible or I am missing something?

miloira commented 2 years ago

Thank you for so fast fixing! I am impressed with your professionalism.

Now it works fine in my local (Windows) computer as part of a Flask app, however, the same app at the Litespeed server it does not works. My app does not start. No error is logged. If I remove the schedule-decorator, then my app works fine.

I guess schedule-decorator is not compatible or I am missing something?

I guess you execute it in main thread, that will lead to the main thread is blocked.

NannoSilver commented 2 years ago

Do you have available a code with an example on how to implement schedule-decorator correctly to work in a web server with WSGI ?

NannoSilver commented 2 years ago

I had a look on how work a background thread with _thread package, but I really do not know how to implement together with schedule-decorator, and I could not find documentation on that.

miloira commented 2 years ago
import threading
from schedule_decorator import cron

x = 0
@cron.every('1','seconds')
def task():
    global x
    x = x + 1
    print(x)

@cron.every('5','seconds')
def task():
    print('------------')

threading.Thread(target=cron.run).start()
NannoSilver commented 1 year ago

Thank you for the sample code, @miloira

I tested it at Cloudlinux/Litespeed server for my Python/Flask web application (app) and found an unexpected situation...

Every time the app starts a new treading is created, and that treading detaches completely from the main thread. Because the app is a web application and the Litespeed server has WSGI, always a new user access, a new WSGI worker is started, that leads to schedule_decorator to start a new threading.

After a 2 days of testing I stopped the app, but all the detached (schedule_decorator) treading kept running. I only discovered that because my test included to save a counter in a TXT file. The data center support team spent a good time to find a way to identify and kill the detached treading.

That issue looks like a bug at Litespeed server, that when the app is stopped, it stops the main thread only and keep the child threading running. Any way, if you have any idea on how to overcome that problem, I will be more than happy to listen!