跳转至

Celery

约 361 个字 1 行代码 1 张图片 预计阅读时间 1 分钟

对异步支持不佳。

如果在函数中读写 MongoDB,需要用同步的方法(不能用 Beanie,而是 bunnet)

在 Windows 上的执行命令:

celery -A src.gpxutil_server.core.celery_conf worker --loglevel=info --pool=solo

必须用 --pool=solo(如果没有会报错):

ValueError: not enough values to unpack (expected 3, got 0)

不能用 -P eventlet(会导致无法初始化数据库框架):

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: connection closed (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 6854353c b6e2e5cf639a3125, topology_type: Unknown, servers: [<ServerDescription ('localhost', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('localhost:27017: connection closed (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>

尝试过 celery -A src.gpxutil_server.core.celery_conf worker --loglevel=info -P threads --concurrency=4,能够运行,但是会警告;SQLite 库要关掉线程检查,否则报错:

sqlite3.connect(self.database, check_same_thread=False)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 22616 and this is thread id 40760.

在 PyCharm 中调试 Celery

添加配置时选 Python。

运行选 module:celery。不要选 Script:celery 路径,会报错:

1
2
3
Traceback (most recent call last): File "C:\Users\dingj\anaconda3\envs\gpxutil\Lib\tokenize.py", line 381, in find_cookie line_string = line.decode('utf-8') ^^^^^^^^^^^^^^^^^^^^ UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 2: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Program Files\JetBrains\PyCharm 2024.3.4\plugins\python-ce\helpers\pydev\pydevd.py", line 1570, in _exec pydev_imports.execfile(file, globals, locals) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\JetBrains\PyCharm 2024.3.4\plugins\python-ce\helpers\pydev_pydev_imps_pydev_execfile.py", line 11, in execfile stream = tokenize.open(file) # @UndefinedVariable ^^^^^^^^^^^^^^^^^^^ File "C:\Users\dingj\anaconda3\envs\gpxutil\Lib\tokenize.py", line 445, in open encoding, lines = detect_encoding(buffer.readline) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\dingj\anaconda3\envs\gpxutil\Lib\tokenize.py", line 422, in detect_encoding encoding = find_cookie(first) ^^^^^^^^^^^^^^^^^^ File "C:\Users\dingj\anaconda3\envs\gpxutil\Lib\tokenize.py", line 386, in find_cookie raise SyntaxError(msg) SyntaxError: invalid or missing encoding declaration for 'C:\Users\dingj\anaconda3\envs\gpxutil\Scripts\celery.exe' python-BaseException

下方填参数。

在 PyCharm 中调试 Celery 的配置项
在 PyCharm 中调试 Celery 的配置项