Permalink
Browse files

new post

  • Loading branch information...
1 parent edfca30 commit 8944a0b9ae0960ced0d7ad86ee6d34df16bd0e00 lepture committed Mar 16, 2012
View
@@ -15,7 +15,7 @@ year_archive_template = year_archive.html
[context]
author = lepture
-;disqus = lepture
+disqus = lepture
sitename = Just lepture
siteurl = http://lepture.com
feed = http://feeds.lepture.com/lepture
View
@@ -246,6 +246,9 @@ h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover {
border-left: 3px solid lighten(@basecolor, 20%);
background: #fff;
}
+.hentry p {
+ line-height: 1.8;
+}
.hentry .section {
padding: 0.8em 0;
margin: 0.4em 0;
View
@@ -42,7 +42,7 @@ It's now powered by `Felix Felicis </project/liquidluck>`_ which is a static blo
by me, written in python. It's not perfect, but I wrote it, I can improve it. The source code
of this site can be found at `Github Blog Source <https://github.com/lepture/lepture.com>`_ .
-If you are interested in this project, you should check this article `Mechanism of Felix Felicis <http://dev.lepture.com/work/mechanism-of-liquidluck/>`_
+If you are interested in this project, you should check this article `Mechanism of Felix Felicis <http://lepture.com/work/mechanism-of-liquidluck/>`_
Elsewhere
View
@@ -10,12 +10,16 @@ Links / 交集
Tools
--------
-+ `Ping <http://blogsearch.google.com/ping?url=http://lepture.com/archive/>`_
++ `Ping Blog <http://blogsearch.google.com/ping?url=http://lepture.com/archive/>`_
+
++ `Ping Python China <http://blogsearch.google.com/ping?url=http://python-china.org/feed>`_
+ `Webmaster <https://www.google.com/webmasters/tools/home>`_
+ `Analytics <https://www.google.com/analytics/settings/home>`_
++ `Python China <http://python-china.org>`_
+
Wuhan University
-----------------
View
@@ -0,0 +1,21 @@
+June
+=============
+
+:template: page.html
+:date: 2012-03-04
+:folder: project
+:public: false
+:nav: home
+:github: lepture/june
+
+
+June is a forum
+
+June for Tornado
+----------------
+
+June for WSGI
+-------------
+
+June for SAE
+------------
@@ -1,5 +1,5 @@
-Fix mysql has gone away with SQLAlchemy in tornado
-===================================================
+MySQL and SQLAlchemy in tornado
+================================
:folder: work
:date: 2012-02-09 21:15
@@ -10,8 +10,10 @@ Fix mysql has gone away with SQLAlchemy in tornado
- tornado
-I just started a project at http://start.lepture.com , which is built on tornado with database
-of mysql.
+Fix MySQL has gone away
+-----------------------
+
+I just started a new project, which is built on tornado with database of mysql.
It's a new project, and there is no user. Therefore, the database can be inactive for a long
time, it then occurred to me that mysql has gone away!
@@ -23,51 +25,65 @@ Then I figured it out, I am using tornado! I can set a PeriodicCallback to ping
pool recycle time, so that mysql can not go away.
I have written a `wrap for SQLAlchemy <http://lepture.com/work/tornado-ext/>`_ to make it a
-little Django like. Then I add this feature to it as:
+little Django like. Then I add this feature to it.
.. sourcecode:: python
- class SQLAlchemy(object):
- """
- Example::
+ if 'pool_recycle' in kwargs:
+ # ping db, so that mysql won't goaway
+ PeriodicCallback(self._ping_db, kwargs['pool_recycle'] * 1000).start()
+
- db = SQLAlchemy("mysql://user:pass@host:port/db", pool_recycle=3600)
+.. sourcecode:: python
- from sqlalchemy import Column, String
+ def _ping_bd(self):
+ self.session.execute('show variables')
- class User(db.Model):
- username = Column(String(16), unique=True, nullable=False)
- password = Column(String(30), nullable=False)
- >>> db.create_db()
- >>> User.query.filter_by(username='yourname')
+Master Slave in SQLAlchemy
+---------------------------
- """
- def __init__(self, database, **kwargs):
- self.engine = create_engine(database, **kwargs)
- self.session = self.create_session()
- self.Model = self.create_model()
- if 'pool_recycle' in kwargs:
- # ping db, so that mysql won't goaway
- PeriodicCallback(self._ping_db, kwargs['pool_recycle'] * 1000).start()
+I hate SQL! I know that orm sucks, but SQL is killing me. I am using SQLAlchemy as the
+orm engine, but it's not that easy to implement in a project. I wrote some snippets to
+make it easy to use. And I introduced a new feature in the snippet today! (2012-02-28)
- def create_session(self):
- session = sessionmaker(bind=self.engine, query_cls=DjangoQuery)
- return scoped_session(session)
+Master and Slave support in SQLAlchemy! What a tremendous feature. There are some answers on
+Stack Overflow, but I thought mine is more elegant (maybe I am wrong).
- def create_model(self):
+.. sourcecode:: python
+
+ @property
+ def Model(self):
+ if hasattr(self, '_base'):
+ base = self._base
+ else:
base = declarative_base(cls=Model, name='Model')
+ self._base = base
+ if self.slaves:
+ slave = random.choice(self.slaves)
+ base.query = slave.query_property()
+ else:
base.query = self.session.query_property()
- return base
+ return base
- def create_db(self):
- return self.Model.metadata.create_all(self.engine)
- def _ping_db(self):
- self.session.execute('show variables')
+You may have a little confuse, have a better understanding with the source code at Github_ .
+**You must read the whole code before continuing!**
+
+After it is integrated in your project! You can read data from slave database with:
+
+``Member.query.filter_by(username='lepture')``
+
+For writing data into master database, using ``db.session.add(model)`` .
+
+For updating data, you should query the model with:
+
+``db.session.query(Member).filter_by(username='lepture')``
+
+Always remember, ``db.session`` is master, ``Model.query`` is slave,
+``Model.query`` is read-only!
-You may have a little confuse, have a better understanding with the source code at Github_ .
.. _document: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html
.. _Github: https://github.com/lepture/tornado.ext/blob/master/database.py
Oops, something went wrong.

0 comments on commit 8944a0b

Please sign in to comment.