如果你需要每X 分鐘/秒等運行一個函數來進行一些清理,觸發一些操作,你可以在線程模組和django 自訂cli 的幫助下做一個簡單的調度程序命令。
假設我想每 5 秒呼叫一個函數以在外部 API 上發布一些內容。
在您的 django 應用程式中建立一個名為 management 的資料夾/包,在該資料夾內建立另一個名為commands 的資料夾。在命令資料夾中建立一個名為 runposter.py 的模組。最後你會得到類似這樣的結構 yourapp/management/commands/runposter.py。
在此程式碼中,我們使用一個運行 while 循環的線程,只要它不每 5 秒停止一次。將 print("posting") 替換為您要執行的函數/邏輯。
# runposter.py import time from threading import Thread, Event from django.conf import settings from django.core.management.base import BaseCommand stop_event = Event() def my_job(): while not stop_event.is_set(): try: print("posting") time.sleep(5) except KeyboardInterrupt: break class Command(BaseCommand): help = "Run Poster." def handle(self, *args, **options): poster = Thread(target=my_job) try: print("Starting poster...") poster.start() while poster.is_alive(): poster.join(timeout=1) except KeyboardInterrupt: print("Stopping poster...") stop_event.set() poster.join() print("Poster shut down successfully!")
很好,現在打開另一個終端機視窗並運行 python manage.py runposter。如您所見,命令 runposter 是根據我們給出的模組名稱建立的。
當然,對於更複雜的東西,我建議使用 rq-scheduler 或 celery 週期性任務或 django-q。
但是,對於簡單的情況,這應該足夠了。
以上是如何在 Django 中創建一個簡單的調度程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

數組的同質性對性能的影響是雙重的:1)同質性允許編譯器優化內存訪問,提高性能;2)但限制了類型多樣性,可能導致效率低下。總之,選擇合適的數據結構至關重要。

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境