Less than 1 minute
- MySQL25
- interview17
- python12
- java11
- ECS10
- Linux10
- nowcoder9
- git6
- go6
- spring6
- Computer Base6
- wsl5
- postgresql4
- Intro4
- 源码分析4
- computer tower4
- Logging Framework3
- c3
- Environment setup3
- redis2
- 架构2
- docker2
- c++2
- milvus2
- wechat2
- MySQL查询深入优化2
- architect1
- Tools1
- icon1
- markdown1
- docker compose1
- Nginx1
- k8s1
- ssh1
- springboot1
- Design Patterns1
- Term1
- CLion1
- windows1
- chocolatey1
- Blog1
- vue1
- trouble shooting1
简介
An extremely fast Python package and project manager, written in Rust.
下载
curl -LsSf https://astral.sh/uv/install.sh | sh
Less than 1 minute
import time
def retry(retry_times: int = 3):
def decorator(func):
def wrapper(*args, **kwargs):
for i in range(retry_times):
try:
return func(*args, **kwargs)
except Exception as e:
print(f"{str(func).split(' ')[1]} retry {i + 1} times cause exception {e}")
if i == retry_times - 1:
raise e
return wrapper
return decorator
def timer():
def decorator(func):
def wrapper(*args, **kwargs):
start = time.perf_counter()
try:
return func(*args, **kwargs)
except Exception as e:
raise e
finally:
end = time.perf_counter()
print(f"{func.__name__} cost {end - start} seconds")
return wrapper
return decorator
Less than 1 minute
用法
# 预先全局声明
current_date = datetime.datetime.now().strftime('%Y-%m-%d')
def a():
global current_date
current_date = '2024-09-05'
if __name__ == '__main__':
print(current_date)
# 输出 2024-09-03
a()
print(current_date)
# 输出 2024-09-05
Less than 1 minute
import pymysql
from dbutils.pooled_db import PooledDB
from config.mysql import DATABASE_CONFIG
# 创建数据库连接池
class MysqlPool(object):
__pool = None
def __enter__(self):
self.conn = self.get_conn()
self.cursor = self.conn.cursor()
return self.conn, self.cursor
def __exit__(self, exc_type, exc_val, exc_tb):
self.cursor.close()
self.conn.close()
@classmethod
def get_conn(cls):
if cls.__pool is None:
cls.__pool = PooledDB(
# 使用链接数据库的模块
creator=pymysql,
# 连接池允许的最大连接数,可以根据情况调整
maxconnections=5,
# 初始化时,链接池中至少创建的空闲的链接,0表示不创建
mincached=5,
# 链接池中最多闲置的链接,0和None不限制
# 链接池中最多共享的链接数量,0和None表示全部共享。
maxcached=5,
# PS: 无用,因为pymysql和MySQLdb等模块的 threadsafety都为1,
# 所有值无论设置为多少,maxcached永远为0,所以永远是所有链接都共享。
maxshared=3,
# 连接池中如果没有可用连接后,是否阻塞等待。True,等待;False,不等待然后报错
blocking=True,
# 一个链接最多被重复使用的次数,None表示无限制
maxusage=None,
# 开始会话前执行的命令列表。如:["set datestyle to ...", "set time zone ..."]
setsession=[],
# ping MySQL服务端,检查是否服务可用。
# 如:0 = None = never,
# 1 = default = whenever it is requested,
# 2 = when a cursor is created,
# 4 = when a query is executed,
# 7 = always
ping=0,
**DATABASE_CONFIG
)
return cls.__pool.connection()
# if __name__ == '__main__':
# with MysqlPool() as (conn, cursor):
# cursor.execute("select * from sina_notices limit 10")
# result = cursor.fetchall()
# print(result)
# if result:
# conn.commit()
# else:
# conn.rollback()
About 1 min
背景
在 Linux 上运行 celery,执行 celery -A _celery worker -l INFO 的时候报错
/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/platforms.py:829: SecurityWarning: You're running the worker with superuser privileges: this is
absolutely not recommended!
Please specify a different user using the --uid option.
User information: uid=0 euid=0 gid=0 egid=0
warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(
-------------- celery@DESKTOP-I2O884V v5.4.0 (opalescent)
--- ***** -----
-- ******* ---- Linux-5.15.153.1-microsoft-standard-WSL2-x86_64-with-glibc2.34 2024-07-18 10:32:43
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: spiders:0x7f0aa7fda520
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: redis://
- *** --- * --- .> concurrency: 16 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. _celery.tasks.add
. _celery.tasks.multiplication
. _celery.tasks.xsum
[2024-07-18 10:32:43,612: CRITICAL/MainProcess] Unrecoverable error: ModuleNotFoundError("No module named '_ctypes'")
Traceback (most recent call last):
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/worker/worker.py", line 202, in start
self.blueprint.start(self)
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/bootsteps.py", line 116, in start
step.start(parent)
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/bootsteps.py", line 365, in start
return self.obj.start()
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/concurrency/base.py", line 130, in start
self.on_start()
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/concurrency/prefork.py", line 109, in on_start
P = self._pool = Pool(processes=self.limit,
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/concurrency/asynpool.py", line 464, in __init__
super().__init__(processes, *args, **kwargs)
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/billiard/pool.py", line 1045, in __init__
self._create_worker_process(i)
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/celery/concurrency/asynpool.py", line 482, in _create_worker_process
return super()._create_worker_process(i)
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/billiard/pool.py", line 1141, in _create_worker_process
on_ready_counter = self._ctx.Value('i')
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/billiard/context.py", line 177, in Value
from .sharedctypes import Value
File "/root/.pyenv/versions/3.8.0/envs/python-3.8.0/lib/python3.8/site-packages/billiard/sharedctypes.py", line 10, in <module>
import ctypes
File "/root/.pyenv/versions/3.8.0/lib/python3.8/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
About 1 min
背景
在 Windows 上运行 celery,执行 res.get(timeout=10) 的时候报错
res.get(timeout=10)
Traceback (most recent call last):
File "C:\Apps\JetBrains\PyCharm 2023.3.3\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode
coro = func()
File "<input>", line 1, in <module>
File "C:\Tools\miniconda3\envs\python3.8\lib\site-packages\celery\result.py", line 251, in get
return self.backend.wait_for_pending(
File "C:\Tools\miniconda3\envs\python3.8\lib\site-packages\celery\backends\asynchronous.py", line 223, in wait_for_pending
return result.maybe_throw(callback=callback, propagate=propagate)
File "C:\Tools\miniconda3\envs\python3.8\lib\site-packages\celery\result.py", line 365, in maybe_throw
self.throw(value, self._to_remote_traceback(tb))
File "C:\Tools\miniconda3\envs\python3.8\lib\site-packages\celery\result.py", line 358, in throw
self.on_ready.throw(*args, **kwargs)
File "C:\Tools\miniconda3\envs\python3.8\lib\site-packages\vine\promises.py", line 235, in throw
reraise(type(exc), exc, tb)
File "C:\Tools\miniconda3\envs\python3.8\lib\site-packages\vine\utils.py", line 27, in reraise
raise value
ValueError: not enough values to unpack (expected 3, got 0)
Less than 1 minute
Less than 1 minute
背景:想要使用命令行执行 python 脚本,但是得到了 ModuleNotFoundError: No module named 'xxx'
项目结构
py-spider
└── util
├── request.py
├── mysql_pool.py
├── logger.py
└── __init__.py
└── spider
├── spider_demo.py
└── __init__.py
└── main.py
About 4 min
管理多个Python版本和它们的虚拟环境对于任何需要在不同项目间切换的开发者来说都是一项基础技能。从官方的 venv 到强大的 pyenv 和其他第三方工具,Python社区提供了一系列的工具来简化这一过程。
本文将为你提供一个全面(现在还不全,未来会全的)的指南,帮助你掌握这些工具的使用方法。
前言
我们首先需要知道,python版本管理工具,python环境管理工具,python包管理工具,这三个之间的区别。
- python版本管理工具:决定了你的计算机默认执行python的版本,如conda,pyenv,当你的不同项目需要不同的python版本的时候你就需要进行多版本管理了
- python环境管理工具:支持创建python虚拟环境,如venv,virtualenv,pipenv等,当你的不同项目需要同一个包的不同版本时,你就需要进行虚拟环境管理了
- python包管理工具:支持python依赖包的管理,如pip,pipenv等,类似于前端的npm,后端的maven
About 7 min
