ericls / django-simple-task Goto Github PK
View Code? Open in Web Editor NEWSimple background task for Django 3
Home Page: https://django-simple-task.rtfd.org
License: MIT License
Simple background task for Django 3
Home Page: https://django-simple-task.rtfd.org
License: MIT License
If I run a fairly simple django app (which I'll try to get pushed to a sample repo on github at some point) that follows your instructions, when running the django app via Daphne, Daphne doesn't send a scope["type"] == lifetime event, and as a result, the queue/loop values don't get set, so when defer is called, the loop doesn't get used and the function just finishes (so the deferred "func" isn't called); swapping uvicorn for Daphne and django-simple-task works as advertised. Reading quickly the ASGI spec, I think Daphne isn't following the suggested lifetime bit, but I could be missing something. Anyways, I would suggest:
A) If you've tested it with Daphne, documenting any extra steps required
B) If I'm correct and uvicorn is doing the asgi spec the way django-simple-task expects, recommend using that.
C) Modifying task.py's defer so that if loop isn't defined, it at least logs that there was no queue to save this func to run (or possibly just call the func inline?)
The following deprecation warning appears for:
RemovedInDjango41Warning:
'django_simple_task' defines default_app_config = 'django_simple_task.apps.DjangoSimpleTaskConfig'.
Django now detects this configuration automatically. You can remove default_app_config.
app_config = AppConfig.create(entry)
Will django-simple-task run for Django-4.0 and Python-3.10?
django-simple-task/.github/workflows/pythonpackage.yml
Lines 10 to 12 in 90d553c
can we do something to impl this func?
my use the call_later?
System:
# asgi.py
import os
from django.core.asgi import get_asgi_application
from django_simple_task import django_simple_task_middlware
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'src.settings')
application = get_asgi_application()
application = django_simple_task_middlware(application)
# views.py
import time
from django.shortcuts import HttpResponse
from django_simple_task import defer
def task1():
time.sleep(1)
print("task1 done")
def view1(requests):
defer(task1)
return HttpResponse("My View")
# urls.py
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path("test", views.view1)
]
Thanks for an interesting project! Perhaps I've missed something, but using the current example in the docs doesn't seem to output or execute the deferred functions when using Daphne, but does with Uvicorn.
Using:
uvicorn's output:
$ uvicorn --log-level debug background_tasks_test.asgi:application
INFO: Started server process [33682]
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:64302 - "GET /test_tasks/ HTTP/1.1" 200 OK
task1 done
INFO: [Thread: 4514371008][Worker: 0]: finished task1.
task2 done
INFO: [Thread: 4514371008][Worker: 0]: finished task2.
…
daphne's output:
$ daphne -v 3 background_tasks_test.asgi:application
2019-12-30 22:33:17,618 INFO Starting server at tcp:port=8000:interface=127.0.0.1
2019-12-30 22:33:17,618 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2019-12-30 22:33:17,618 INFO Configuring endpoint tcp:port=8000:interface=127.0.0.1
2019-12-30 22:33:17,619 INFO HTTPFactory starting on 8000
2019-12-30 22:33:17,619 INFO Starting factory <daphne.http_protocol.HTTPFactory object at 0x110678c10>
2019-12-30 22:33:17,619 INFO Listening on TCP address 127.0.0.1:8000
2019-12-30 22:33:25,794 DEBUG HTTP b'GET' request for ['127.0.0.1', 64312]
2019-12-30 22:33:25,898 WARNING Executing <Handle callWithLogger(<_GenericHTTP...ol #0 on 8000>, <bound method... 0x110399ac0>>, <_GenericHTTP...ol #0 on 8000>, True) at …/background_tasks_test/virtualenv/lib/python3.8/site-packages/twisted/python/log.py:90 created at /opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/selector_events.py:252> took 0.173 seconds
2019-12-30 22:33:26,040 DEBUG HTTP 200 response started for ['127.0.0.1', 64312]
2019-12-30 22:33:26,040 DEBUG HTTP close for ['127.0.0.1', 64312]
2019-12-30 22:33:26,040 INFO "127.0.0.1" - - [01/Jan/1970:00:00:10 +0000] "GET /test_tasks/ HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
2019-12-30 22:33:26,041 DEBUG HTTP response complete for ['127.0.0.1', 64312]
127.0.0.1:64312 - - [30/Dec/2019:22:33:26] "GET /test_tasks/" 200 7
…
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.