Closed TafkaMax closed 1 year ago
If you are trying to mimic:
def job(self, interval):
def decorator(f):
self._add_job(f, interval)
return f
return decorator
but where you are using the fact that the class instance can be callable, it would be:
def __call__(self, interval):
def decorator(f):
self._add_job(f, interval)
return f
return decorator
I am not even sure a wrapt type decorator is even needed as the function called decorator
here isn't really a decorator in the sense of fulfilling a role of providing a wrapper that is called each time the function is called, instead it is performing a one off action when the code file is first parsed and processed, with the original function still being returned.
Naming wise it is possibly better written as the following so intent is clearer and the name decorator
being used doesn't confuse things.
def __call__(self, interval):
def add_job(f):
self._add_job(f, interval)
return f
return add_job
Hi
Thanks for the insight.
In the end I didn't use the decorator function and opted for another way to do this.
I renamed the SynchronizeThreadedTask to BackgroundTasks and this seems to work. I am using another version of timeloop
https://github.com/Ruggiero-Santo/timeloop
class BackgroundTasks(threading.Thread):
"""Class that runs background tasks for a flask application.
"""
def __init__(self, app: Flask, timeloop: Timeloop): -> None
super(BackgroundTasks, self).__init__()
self.app = app : Flask
self.timeloop = timeloop : Timeloop
self.timeloop.add_job(self.synchronize_applications, interval=timedelta(minutes=1), exception=False)
def run(self): -> None
# Use the current application context and start the timeloop service.
with self.app.app_context():
self.timeloop.start()
def synchronize_applications(self): -> bool
In the end the current solution works, but I will try to look at ways to incorporate using decorators instead of writing inside the init function.
Nevertheless this whole journey at decorators has broadened my knowledge on the topic and it's usages and how it is commonly misunderstood. Thanks for the wrapt package :) .
Hi
This post isn't about a specific bug rather I need advice.
I am trying to use: https://github.com/sankalpjonn/timeloop
Or rather a version of it and developing a new version from it.
I am trying to create a job factory inside and it is actually really difficult.
First I create the object that stores the necessary jobs.
Below is my create_app factory for flask. Here I import the created Timeloop object and send it to another thread.
Here is the secondary thread that should run in flask background, but gets all the configuration/context of the flask app.
And below is my attempt at using wrapt.
Running this creates the following error
In the documentation there isn't much said about my use case.
Class instance that contains a decorator that is used on a another classes instance function.