python – Gearman SQLAlchemy – 继续失去MySQL线程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Gearman SQLAlchemy – 继续失去MySQL线程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2219字,纯文字阅读大概需要4分钟。
内容图文
![python – Gearman SQLAlchemy – 继续失去MySQL线程](/upload/InfoBanner/zyjiaocheng/905/97f05f46802740ceba404560a75fc974.jpg)
我有一个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 – Gearman SQLAlchemy – 继续失去MySQL线程全部内容,希望文章能够帮你解决python – Gearman SQLAlchemy – 继续失去MySQL线程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。