我想在RQ工作器(rq_worker.py)中重用数据库连接。此连接在预加载库(task.py)中建立。之后,将两个作业添加到RQ队列(rq_queue.py)。
如果我不使用Docker,一切都很好。但是,如果我在容器中运行MySQL:
我已经尝试增加max_allowed_packet
(https://dba.stackexchange.com/questions/40899/mysql-error-reading-communication-packets)失败
# rq_worker.py
from redis import Redis
from rq import Connection, Worker
# Preload library
import task
with Connection(Redis()):
w = Worker('default')
w.work()
# task.py
import mysql.connector
mysql_conn = mysql.connector.connect(
user='_user', password='_password', database='_database')
def print_mysql_version():
cursor = mysql_conn.cursor()
cursor.execute("SELECT version();")
print(cursor.fetchone())
cursor.close()
# rq_queue.py
from redis import Redis
from rq import Queue
from task import print_mysql_version
task_queue = Queue(connection=Redis())
task_queue.enqueue(print_mysql_version)
task_queue.enqueue(print_mysql_version)
没有Docker:
sudo mysql -uroot -e"create database _database; \
create user _user@localhost identified by '_password'; \
grant all on _database.* to _user@localhost;"
RQ worker 'rq:worker:ipa.4109' started, version 0.13.0
Listening on default...
Cleaning registries for queue: default
default: task.print_mysql_version() (22649b0b-1089-4729-9995-8c850bb7c49b)
('5.7.25-0ubuntu0.18.04.2',)
default: Job OK (22649b0b-1089-4729-9995-8c850bb7c49b)
Result is kept for 500 seconds
default: task.print_mysql_version() (60fca63f-e0ad-4868-ac1e-637edf00b226)
('5.7.25-0ubuntu0.18.04.2',)
default: Job OK (60fca63f-e0ad-4868-ac1e-637edf00b226)
Result is kept for 500 seconds
使用Docker
docker run -p 3306:3306 -e MYSQL_DATABASE=_database -e MYSQL_USER=_user \
-e MYSQL_PASSWORD=_password -e MYSQL_ALLOW_EMPTY_PASSWORD=yes \
mysql:5.7.25
RQ worker 'rq:worker:ipa.5080' started, version 0.13.0
*** Listening on default...
Cleaning registries for queue: default
default: task.print_mysql_version() (1b53a0d5-6a0e-4704-a3f5-4cc7f0cb71d7)
('5.7.25',)
default: Job OK (1b53a0d5-6a0e-4704-a3f5-4cc7f0cb71d7)
Result is kept for 500 seconds
default: task.print_mysql_version() (f9c2928d-3d84-4041-b5bb-e650f43f05e1)
mysql.connector.errors.OperationalError: MySQL Connection not available.
Traceback (most recent call last):
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/rq/worker.py", line 799, in perform_job
rv = job.perform()
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/rq/job.py", line 600, in perform
self._result = self._execute()
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/rq/job.py", line 606, in _execute
return self.func(*self.args, **self.kwargs)
File "/home/user/projects/patfinder/rq_test/task.py", line 10, in print_mysql_version
cursor = mysql_conn.cursor()
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 447, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.
Traceback (most recent call last):
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/rq/worker.py", line 799, in perform_job
rv = job.perform()
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/rq/job.py", line 600, in perform
self._result = self._execute()
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/rq/job.py", line 606, in _execute
return self.func(*self.args, **self.kwargs)
File "/home/user/projects/patfinder/rq_test/task.py", line 10, in print_mysql_version
cursor = mysql_conn.cursor()
File "/home/user/projects/patfinder/venv_prod/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 447, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.
Moving job to 'failed' queue
2 [Note] Aborted connection 2 to db: '_database' user: '_user' host: '172.17.0.1' (Got an error reading communication packets)