数据库
首页 > 数据库> > python – Gearman SQLAlchemy – 继续失去MySQL线程

python – Gearman SQLAlchemy – 继续失去MySQL线程

作者:互联网

我有一个python脚本,设置了几个齿轮工人.他们在我有的SQLAlchemy模型上调用了一些方法,它们也被Pylons应用程序使用.

一切都工作正常一两个小时,然后MySQL线程丢失,所有查询都失败.当我为pool_recycle定义这么低的值时,我无法弄清楚为什么线程会丢失(我在3个不同的服务器上得到相同的结果).另外,为什么不创建新连接?

有什么想法可以调查吗?

import gearman
import json
import ConfigParser
import sys
from sqlalchemy import create_engine

class JSONDataEncoder(gearman.DataEncoder):
    @classmethod
    def encode(cls, encodable_object):
        return json.dumps(encodable_object)
    @classmethod
    def decode(cls, decodable_string):
        return json.loads(decodable_string)

# get the ini path and load the gearman server ips:ports
try:
    ini_file = sys.argv[1]
    lib_path = sys.argv[2]
except Exception:
    raise Exception("ini file path or anypy lib path not set")

# get the config
config = ConfigParser.ConfigParser()
config.read(ini_file)
sqlachemy_url =  config.get('app:main', 'sqlalchemy.url')
gearman_servers =  config.get('app:main', 'gearman.mysql_servers').split(",")

# add anypy include path
sys.path.append(lib_path)
from mypylonsapp.model.user import User, init_model
from mypylonsapp.model.gearman import task_rates

# sqlalchemy setup, recycle connection every hour
engine = create_engine(sqlachemy_url, pool_recycle=3600)
init_model(engine)

# Gearman Worker Setup
gm_worker = gearman.GearmanWorker(gearman_servers)
gm_worker.data_encoder = JSONDataEncoder()

# register the workers
gm_worker.register_task('login', User.login_gearman_worker)
gm_worker.register_task('rates', task_rates)

# work
gm_worker.work()

解决方法:

无论使用哪个DB库,我都已经看到了Ruby,PHP和Python的全面内容.我找不到如何解决这个使用mysql_ping的“正确”方法,但是有一个SQLAlchemy解决方案,这里解释得更好http://groups.google.com/group/sqlalchemy/browse_thread/thread/9412808e695168ea/c31f5c967c135be0

正如该线程中有人指出的那样,将recycle选项设置为True等同于将其设置为1.更好的解决方案可能是找到MySQL连接超时值并将回收阈值设置为80%.

您可以通过查找此变量http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_connect_timeout从实时集中获取该值

编辑:
   我找了一些关于使用pool_recycle的authoritivie文档
   http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/connections.html?highlight=pool_recycle

标签:python,mysql,sqlalchemy,pylons,gearman
来源: https://codeday.me/bug/20190630/1340256.html