Source: Python 以 ctypes 載入 DLL、SO 動態連結函式庫教學與範例
Python 的 ctypes
模組可以用來呼叫外部的函式庫,提供相容於 C 語言的資料型別,開發者可以透過這個模組直接呼叫 DLL 檔案(Windows 平台)或 SO 檔案(macOS 或 Linux 平台)中的函數。以下分別介紹 Windows 平台建立與使用 DLL 檔案、macOS 與 Linux 平台建立與使用 SO 檔案的步驟。
Source: Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤
PyAutoGUI 是一套可以控制電腦滑鼠與鍵盤的 Python 模組,適合用來將重複性的人工滑鼠或鍵盤操作自動化,甚至可以用於撰寫簡單的遊戲外掛程式。
Continue reading »cpu stress:
from multiprocessing import Pool
from multiprocessing import cpu_count
def f(x):
while True:
x*x
if __name__ == '__main__':
processes = cpu_count()
print('utilizing %d cores\n' % processes)
pool = Pool(processes)
pool.map(f, range(processes))
memory stress:
# Eats about 1GB of memory
x = bytearray(1*1024*1024*1024)
Source:
- https://stackoverflow.com/questions/6317818/eat-memory-using-python
- https://gist.github.com/tott/3895832
- https://stackoverflow.com/questions/35312756/how-can-i-simulate-cpu-and-memory-stress-in-python
Source: multiprocessing 模組的注意事項
Python 雖然是跨平台的程式語言, 不過實際上特定的功能還是會因為平台的差異, 產生不同的結果, 如果沒有特別注意, 就會對執行結果感到訝異, multiprocessing 就是一個明顯的例子。請看以下這個簡單的程式:
Continue reading »Source: MicroPython umqtt 連線/斷線的的注意事項
MicroPython 提供有 umqtt 模組可以使用 MQTT 協定, 不過在 umqtt 裡面有再分為 simple 和 robust 模組, 這兩種模組功能基本相同, 但如果設計純訂閱端 (只收資料不送資料) 這樣的程式, 在使用上就必須多加留意。
Continue reading »My preferred indent for a method with multiple parameters and type hinting:
class Config:
def __init__(
self,
default_settings: dict,
conf_path: str = "",
) -> None:
self.settings = default_settings
Continue reading » Source: [Python] 使用 conda 建立及管理虛擬環境
前言
之前在開發 Python 程式時,我都是使用 pip 來做套件管理,使用 virtualenv 來建立虛擬環境,後來發現 Conda 這個工具,Conda 是一個開源的套件及虛擬環境管理工具,支援多種程式語言(Python, R…等),這邊來記錄一下它的使用方式,以下的指令是以 Windows 環境為主,Linux 或 MacOS 的指令可以參考官方文件。
Continue reading »Source: 深入 GIL: 如何寫出快速且 thread-safe 的 Python – Grok the GIL: How to write fast and thread-safe Python
本文將會探討 Python 內部的 Global Interpreter Lock,以及學習其如何影響 multi-threaded 程式。
Continue reading »