我想在自定义类中使用带有sqlAlchemy的多处理模块。 这是代码:
from sqlalchemy import create_engine
engine = create_engine(f'mysql+pymysql://a:b@localhost:3306/', server_side_cursors=True, pool_size=20)
class Client(object):
def __init__(self):
self.engine = create_engine(f'mysql+pymysql://a:b@localhost:3306/', server_side_cursors=True, pool_size=20)
self.pool = Pool(6)
def run_in_process(self, x):
conn = self.engine.connect()
print(conn)
def run(self):
x = 'x'
res = self.pool.apply_async(self.run_in_process, (x,))
res.get()
def __getstate__(self):
self_dict = self.__dict__.copy()
del self_dict['pool']
return self_dict
def __setstate__(self, state):
self.__dict__.update(state)
pool = Pool(6)
client = Client()
client.run()
显示错误:
File "test_pickle.py", line 32, in <module>
client.run()
File "test_pickle.py", line 19, in run
res.get()
File "/home/airflow/.pyenv/versions/3.7.3/lib/python3.7/multiprocessing/pool.py", line 657, in get
raise self._value
File "/home/airflow/.pyenv/versions/3.7.3/lib/python3.7/multiprocessing/pool.py", line 431, in _handle_tasks
put(task)
File "/home/airflow/.pyenv/versions/3.7.3/lib/python3.7/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/home/airflow/.pyenv/versions/3.7.3/lib/python3.7/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: can't pickle _thread._local objects
我知道有时候由于腌制而造成的麻烦,而且我知道这个问题是由于 self.engine 造成的,因为不能腌制。但是我必须在类中使用engine
这个变量。
那么,在我的示例中如何使引擎可以选择?
谢谢。