很多公司都大量使用了python,其中有一些开发规范,code guidline, 通用组件,基础框架是可以共用的。
每个公司都自己搞一套, 太浪费人力,我想开一帖和大家讨论一下这些python基础设施的搭建。
原则是我们尽量不重新发明轮子,但开源组件这么多,也要有个挑选的过程和组合使用的过程,在这里讨论一下。
另一方面,有些开源组件虽然强大,但我们不能完全的驾驭它,或只使用其中很少的一部分,我们就可以考虑用python实现一个简单的轮子,可控性更强,最好不要超过300行代码。
| <?php | |
| require 'vendor/autoload.php'; | |
| $resolver = new Illuminate\Database\ConnectionResolver; | |
| $resolver->setDefaultConnection('default'); | |
| $factory = new Illuminate\Database\Connectors\ConnectionFactory; | |
| $connection = $factory->make(array( | |
| 'host' => 'localhost', | |
| 'database' => 'database', |
| """ | |
| This is a simple example of WebSocket + Tornado + Redis Pub/Sub usage. | |
| Do not forget to replace YOURSERVER by the correct value. | |
| Keep in mind that you need the *very latest* version of your web browser. | |
| You also need to add Jacob Kristhammar's websocket implementation to Tornado: | |
| Grab it here: | |
| http://gist.github.com/526746 | |
| Or clone my fork of Tornado with websocket included: | |
| http://github.com/pelletier/tornado | |
| Oh and the Pub/Sub protocol is only available in Redis 2.0.0: |
| #!/bin/bash | |
| # Source: http://toomuchdata.com/2012/06/25/how-to-install-python-2-7-3-on-centos-6-2/ | |
| # Install stuff # | |
| ################# | |
| # Install development tools and some misc. necessary packages | |
| yum -y groupinstall "Development tools" | |
| yum -y install zlib-devel # gen'l reqs |
分布式系统的核心是分布式通信,而传统上开发一套支持上千台规模集群,可靠性非常高的分布式通信框架,需要不少的精力投入。而在多数情景下,我们(特别是时间宝贵的OP)并不是非常关注技术实现的细节,而是希望有一套成熟、轻量、可靠性高、使用方便而且易于调试的分布式通信框架,可以直接使用,从而把时间放在具体业务逻辑上。
在PyCon 2012大会上,dotcloud公司开源了一套基于ZeroMQ和MessagePack的分布式通信框架(或者说是协议+Python实现)。该框架因为基于ZeroMQ,使用方法是RPC,所以被命名为ZeroRPC。ZeroRPC的特点在其官网的介绍中一目了然[1]:
ZeroRPC is a light-weight, reliable and language-agnostic library for distributed communication between server-side processes.
| ;; Usage Example: | |
| ;; | |
| ;; <!-- BEGIN RECEIVE ORGTBL ${1:YOUR_TABLE_NAME} --> | |
| ;; <!-- END RECEIVE ORGTBL $1 --> | |
| ;; | |
| ;; <!-- | |
| ;; #+ORGTBL: SEND $1 orgtbl-to-gfm | |
| ;; | $0 | | |
| ;; --> |
| # -*- coding: utf-8 -*- | |
| from twisted.enterprise import adbapi | |
| from twisted.python import log | |
| import MySQLdb | |
| import itertools | |
| class ReconnectingMixin: | |
| """MySQL 重新连接时, ConnectionPool 可以正确执行指定的操作,流程是: | |
| - 执行操作 |
| from gevent import monkey | |
| monkey.patch_socket() | |
| import logging | |
| import gevent | |
| from gevent.queue import Queue | |
| import pymysql as db | |
| logging.basicConfig(level=logging.DEBUG) | |
| LOGGER = logging.getLogger("connection_pool") |
| #!/bin/bash | |
| # How to install PHP memcached on CentOS 6.5 | |
| # Install dependencies | |
| yum install cyrus-sasl-devel zlib-devel gcc-c++ | |
| # Get the latest libmemcached | |
| wget https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemcached-1.0.16.tar.gz | |
| tar -xvf libmemcached-1.0.16.tar.gz |