การเขียนและเรียกใช้ @shared_task
เรียกใช้ฟังก์ชันแบบอะซิงโครนัสด้วย delay()
การเขียนและเรียกใช้ @shared_task เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Tasks Are Just Functions
A Celery task is an ordinary Python function with a decorator on top. The decorator teaches Celery how to queue and run it later.
Why shared_task
Use @shared_task instead of @app.task so your tasks do not depend on a specific Celery app. It is the standard choice inside Django apps.
Write Your First Task
Put tasks in a tasks.py inside an app. Decorate a plain function and Celery picks it up via autodiscover.
from celery import shared_task
@shared_task
def add(x, y):
return x + yCall It with delay
To run a task in the background, call .delay() instead of calling the function directly. It queues the job and returns at once.
add.delay(2, 3) # queued, runs on a workerdelay vs Calling Directly
Calling add(2, 3) runs it right now in your web process. Calling add.delay(2, 3) sends it to the queue. Same function, very different timing.
Pass Simple Arguments
Arguments are serialized to JSON and sent to the broker, so pass simple values like IDs and strings, not whole model objects.
send_email.delay(user.id) # good
send_email.delay(user) # avoid: not serializableThe AsyncResult Handle
delay() returns an AsyncResult. You can store its id and later check status or fetch the return value from the result backend.
result = add.delay(2, 3)
print(result.id)More Control with apply_async
Need a countdown or a specific queue? Use apply_async(), the fuller version of delay with extra options.
add.apply_async((2, 3), countdown=10) # run after 10sRetry on Failure
Bind a task and call self.retry() to re-run it after an error, perfect for flaky network calls that may succeed next time.
@shared_task(bind=True, max_retries=3)
def fetch(self):
try:
call_api()
except Exception as e:
raise self.retry(exc=e)Idempotent Tasks Win
Because tasks can retry, aim to make them idempotent: running twice should be safe and not double-charge or double-send. 🔁
Watch the Worker Log
When you call delay, watch the worker terminal. You will see the task received and its result, which is the fastest way to debug. 👀
Quick Check
How do you run a Celery task in the background?
Recap: Writing Tasks
Decorate a function with @shared_task, call it with .delay() to queue it, pass simple ids, and design tasks to retry safely. 🎯
คำถามที่พบบ่อย
บทเรียน “การเขียนและเรียกใช้ @shared_task” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเขียนและเรียกใช้ @shared_task” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเขียนและเรียกใช้ @shared_task”
เรียกใช้ฟังก์ชันแบบอะซิงโครนัสด้วย delay() คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การเขียนและเรียกใช้ @shared_task” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุผลที่ต้องใช้ทาสก์เบื้องหลัง
- การตั้งค่า Celery ด้วยโบรกเกอร์
- การเขียนและเรียกใช้ @shared_task
- งานตามกำหนดเวลาด้วย Celery Beat