pip install requests2025/02/14
使用 Python Azure SDK 連接 Azure Blob Storage
pip install azure-storage-blob python-dotenv azure-core2023/09/16
FastAPI 透過Celery 處理Async task並透過Docker運行
Celery 是一個強大的分散式任務佇列框架,用於處理非同步任務和分散式任務排程。它在許多應用程式和系統中都有廣泛的用途,包括以下方面:
非同步任務處理:Celery 最常見的用途之一是執行非同步任務。這些任務可以是長時間運行的操作,例如圖像處理、文件上傳、電子郵件發送、資料處理等。透過將這些任務非同步執行,應用程式可以保持響應性,而不會阻塞使用者介面或主要應用程式流程。
定時任務:Celery 可用於建立和執行定時任務,例如每小時執行一次數據備份或每天發送電子郵件報告。透過 Celery 的定時任務功能,您可以輕鬆管理和排程這些重複性任務。
分散式任務:Celery 是一個分散式任務佇列,可用於在多台伺服器或容器之間分發任務。這對於處理大量任務或需要負載平衡的任務非常有用。
訊息處理:Celery 可以用作訊息處理系統,用於處理和分發訊息。這在實時應用程式中非常有用,例如即時聊天或通知系統。
背景處理:在 Web 應用程式中,Celery 可以處理背景任務,例如處理使用者提交的表單資料、生成報告、執行搜尋索引更新等。這有助於提高應用程式的效能和可擴展性。
任務重試和錯誤處理:Celery 具有內建的任務重試和錯誤處理機制,可以自動重試失敗的任務,或將失敗的任務記錄下來以供後續分析和修復。
分散式資料處理:對於需要分散式資料處理的應用程式,Celery 可以幫助將資料處理任務分發到多個節點,以加速資料處理和分析。
非同步通訊:Celery 不僅可以執行任務,還可以用作非同步通訊的機制。應用程式可以使用 Celery 發送訊息、通知其他服務或元件執行操作。
總之,Celery 是一個多功能的工具,適用於需要非同步、分散式、定時或重複性任務處理的各種應用程式。它具有廣泛的用途,可以與多種訊息代理(如 RabbitMQ、Redis 等)和框架集成,以滿足不同應用場景的需求。無論是 Web 開發、資料處理、實時應用程式還是背景任務處理,Celery 都提供了強大的功能和工具,使開發人員能夠更輕鬆地處理各種任務和非同步操作。
2023/09/10
使用Python 取得Windows 某個資料夾中 mp3音源長度
[
26.808,
31.536,
13.8,
9.096,
8.088,
9.48,
3.672,
3.408,
7.224,
5.112,
9.288,
5.352,
3.072,
3.816,
2.592,
3.624,
2.976,
5.016,
2.448,
2.832,
4.824,
3.912,
2.928,
2.472,
2.328,
7.248,
6.672,
3.576,
4.56,
5.688,
4.536,
5.952,
9.504,
5.136,
3.408,
8.16,
8.112,
4.392,
2.64,
3.936,
7.872,
7.272,
5.232,
9.648,
4.152,
3.576,
18.456,
5.64,
3.912,
2.304,
3.96,
5.04,
10.176
]
2023/05/24
Python ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with LibreSSL 2.8.3.
brew install openssl@1.1
pip install urllib3==1.26.6
2023/05/15
2022/03/19
2018/08/13
Python multiprocessing and thread
Thread
import threading
from datetime import datetime
def show(n, s):
total = 0
from random import random
for i in range(n):
total += i
print('total={} start={} end={}'.format(total, str(s), str(datetime.now())))
if __name__ == "__main__":
max = 1000
jobs = []
for index in range(4):
s = datetime.now()
jobs.append(threading.Thread(target=show, args=[max, s]))
for index in range(4):
jobs[index].start()
for index in range(4):
jobs[index].join()
total=499500 start=2018-08-13 14:32:26.588980 end=2018-08-13 14:32:26.590985
total=499500 start=2018-08-13 14:32:26.588980 end=2018-08-13 14:32:26.591987
total=499500 start=2018-08-13 14:32:26.589481 end=2018-08-13 14:32:26.592990
total=499500 start=2018-08-13 14:32:26.589481 end=2018-08-13 14:32:26.593993
Python install Parse Server API
API_ROOT = os.environ.get('PARSE_API_ROOT') or 'https://api.parse.com/1'
修改成
API_ROOT = os.environ.get('PARSE_API_ROOT')
用pip進行安裝
sudo pip3 install git+https://github.com/xxxxxx/ParsePy.git
import os
os.environ["PARSE_API_ROOT"] = "https://your.domain.com/parse"
import datetime
from parse_rest.datatypes import Object
from parse_rest.connection import register
class Student(Object):
pass
APPLICATION_ID = 'AppID'
REST_API_KEY = 'RestKey'
MASTER_KEY = 'MasterKey'
register(APPLICATION_ID, REST_API_KEY, master_key=MASTER_KEY)
s = Student.Query.get(objectId='xxxxaaaa10')
s.name = "C.Y.Fang"
s.math = 60
s.eng = 50
s.cht = 61
s.save()
參考資料:
https://github.com/milesrichardson/ParsePy
Raspberry Pi Startup run Python script
sudo chmod 755 xxx.py
sudo nano /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
sudo python xxx.py
exit 0
2018/08/04
Install BlueZ on the Raspberry Pi
sudo systemctl stop bluetooth
sudo apt-get update
sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev libdbus-glib-1-dev unzip libcap2-bin
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.44.tar.xz
tar xf bluez-5.44.tar.xz
mv bluez-5.44 bluez
cd bluez
./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --enable-library
make
sudo make install
sudo ln -svf /usr/libexec/bluetooth/bluetoothd /usr/sbin/
sudo install -v -dm755 /etc/bluetooth
sudo install -v -m644 src/main.conf /etc/bluetooth/main.conf
sudo systemctl daemon-reload
sudo systemctl start bluetooth
bluetoothd --version
sudo setcap 'cap_net_raw,cap_net_admin+eip' `which hcitool`
getcap !$
參考資料:
https://github.com/getsenic/gatt-python
Bluetooth LE scan as non root?
ImportError: No module named paho.mqtt.client
cd /usr/lib/python2.7/dist-packages
sudo ln -s /home/pi/.local/lib/python2.7/site-packages/paho
參考資料:
https://stackoverflow.com/questions/48752469/cannot-run-paho-mqtt-client-importerror-no-module-named-paho-mqtt-client
2018/05/30
Linux 更換Python版本
#Python
sudo rm -fr /usr/bin/python
ln -s /usr/bin/python3.5 /usr/bin/python
#pip
sudo rm -fr /usr/bin/pip
ln -s /usr/bin/pip3 /usr/bin/pip
方法二
update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
Install OpenCV 3.4.1 on Raspberry Pi Zero W
先輸入下方指令更新
sudo apt-get update
sudo apt-get upgrade
安裝編譯需要使用到的套件
sudo apt-get install -y build-essential cmake pkg-config
接著再安裝OpenCV會用到的套件
sudo apt-get -y install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libatlas-base-dev gfortran python3.5-dev
接著下載OpenCV
2017/11/06
Ubuntu 16.04.3 LTS 安裝Jupyter
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install -y python3.6
sudo rm -fr /usr/bin/python3
sudo rm -fr /usr/bin/python3m
sudo ln -s /usr/bin/python3 /usr/bin/python3.6
sudo ln -s /usr/bin/python3m /usr/bin/python3.6m
安裝pip3
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
2017/09/28
Windows 10 安裝PyQt4
安裝好Python3.4.xxx,並將Python3.4.xxx加入環境變數
再安裝PyQt4-4.11.4-gpl-Py3.4-Qt4.8.7-x64.exe就可以用了
將程式碼複製並儲存成test.py
2016/05/26
使用py2app將python轉成 OS X App
sudo easy_install -U py2app
#產生setup.py
py2applet --make-setup MyApp.py
#清空之前編譯的檔案
rm -rf build dist
#產生OS X應用程式
#檔案dist底下的
python setup.py py2app
參考資料:
Tutorial
2015/09/22
Python urlopen TypeError: the JSON object must be str, not 'bytes'
原來的程式碼為:
# -*- coding: utf-8 -*- from urllib import request url = "http://data.kaohsiung.gov.tw/Opendata/DownLoad.aspx?Type=2&CaseNo1=AP&CaseNo2=17&FileType=2&Lang=C&FolderType=" data = request.urlopen(url).read()
只要將read後方加上decode('utf8')即可
# -*- coding: utf-8 -*-
from urllib import request
url = "http://data.kaohsiung.gov.tw/Opendata/DownLoad.aspx?Type=2&CaseNo1=AP&CaseNo2=17&FileType=2&Lang=C&FolderType="
data = request.urlopen(url).read().decode('utf8')
print (data)
Python 3.5 取得JSON檔案
# -*- coding: utf-8 -*-
import json
from urllib import request
url = "http://data.kaohsiung.gov.tw/Opendata/DownLoad.aspx?Type=2&CaseNo1=AP&CaseNo2=17&FileType=2&Lang=C&FolderType="
data = request.urlopen(url).read().decode("utf-8")
print (json.loads(data))
執行結果:
參考資料:
https://docs.python.org/3.5/library/urllib.request.html#module-urllib.request
https://docs.python.org/3.5/library/json.html
2015/08/21
Django 取得網址列
程式碼:
from django.http import HttpResponse
def get_current_url(request):
return {'url': request.get_full_path()}

