BUG - setting `task_execution` to main does not seem to work.
See original GitHub issueDescribe the bug I want to have periodic tasks running in my main thread, but it does not seem to work. if initialized with nothing, rocketry will:
- attempt to run the tasks as async
- fail on the tasks because they cannot be async
this is ok because the log messages are helpful in suggesting that I should initialize the Rocketry app with `Rocketry(task_execution=‘async’) or something.
Problem is that when I initialize with (task_execution=‘main’), the startup fails. It seems that the log message is not up to date because docs suggest using ```python config={ ‘task_execution’: ‘main’ }
but this has apparently no affect on the configuration (see logs below)
**To Reproduce**
Here is the code I've used to initialize the tasks:
```python
from api.core.management.jobs import something_else
from api.core.management.jobs import something
from django.core.management.base import BaseCommand
import logging
from rocketry import Rocketry
from rocketry.conds import daily
from rocketry import Rocketry, Session
from rocketry.conds import daily, every
from rocketry.log import MinimalRecord
from redbird.repos import CSVFileRepo
class JobLogger(MinimalRecord):
exc_text: Optional[str] = Field(description="Exception text")
repo = CSVFileRepo(filename="/tmp/api-logs/jobs.csv", model=JobLogger)
scheduler : Rocketry = Rocketry(
logger_repo=repo,
config={
'task_execution': 'main'
}
)
# i've attempted to set scheduler.session.config.task_execution to `main` here, but to no avail
@scheduler.task(daily.at("00:10"), execution='main')
def do_something() -> None:
logging.getLogger(__name__).info("Running do_something()")
something.run()
@scheduler.task(daily.at("23:55"), execution='main')
def do_something_else() -> None:
logging.getLogger(__name__).info("Running do_something_else()")
something_else.run()
class Command(BaseCommand):
help = "Setup the periodic jobs runner"
def handle(self, *args, **options):
scheduler.run()
Expected behavior Run the tasks in my main thread and not have the app crash on startup.
Screenshots
/usr/local/lib/python3.10/site-packages/rocketry/session.py:73: FutureWarning: Default execution will be changed to 'async'. To suppress this warning, specify task_execution, ie. Rocketry(task_execution='async')
and then later on, when the job starts:
Traceback (most recent call last):
File ""/usr/local/lib/python3.10/site-packages/rocketry/core/task.py"", line 536, in _run_as_async
output = await self.execute(**params)
File ""/usr/local/lib/python3.10/site-packages/rocketry/tasks/func.py"", line 234, in execute
output = func(**params)
File ""/server/api/core/management/commands/initjobs.py"", line 40, in do_something
do_something.run()
File ""/server/api/core/management/jobs/do_something.py"", line 46, in run
for value in fetched_from_database:
File ""/usr/local/lib/python3.10/site-packages/django/db/models/query.py"", line 394, in __iter__
self._fetch_all()
File ""/usr/local/lib/python3.10/site-packages/django/db/models/query.py"", line 1866, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File ""/usr/local/lib/python3.10/site-packages/django/db/models/query.py"", line 87, in __iter__
results = compiler.execute_sql(
File ""/usr/local/lib/python3.10/site-packages/django/db/models/sql/compiler.py"", line 1393, in execute_sql
cursor = self.connection.cursor()
File ""/usr/local/lib/python3.10/site-packages/django/utils/asyncio.py"", line 24, in inner
raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async."
Desktop (please complete the following information):
Python version: 3.10.7
Rocketry version: 2.4.1
Django version: 4.1
Additional context N/A
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
That would be awesome! I’m personally not qualified for writing the documentation regarding how to integrate Rocketry with Django (as I haven’t used Django myself) but as it is so used, documentation regarding Rocketry+Django would extend Rocketry’s audience quite a lot.
Awesome, will try to work on it by the end of the week. I have exams this week so I might be busy (& therefore a bit late) but i’ll do my best.
Regards