码迷,mamicode.com
首页 > 数据库 > 详细

python 内存数据库与远程服务

时间:2015-12-29 13:02:24      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

python 内存数据库与远程服务


需要import的python 内存数据库代码参考下面的链接:

http://blog.csdn.net/ubuntu64fan/article/details/50424683


现在的问题:

创建一个内存数据服务,可以供本地或远程用户连接上来,多个用户同时并发读写

这个内存数据库。

解决的步骤有3:

(1) 创建服务器(memdb_server.py)

(2) 创建客户端(memdb_client.py)

(3) 启动服务器,启动客户端

1 服务器代码:

#!/usr/bin/python
#-*- coding: UTF-8 -*-
# memdb_server.py
#
# Queues are thread and process safe.
# Refer:
#   https://docs.python.org/2/library/multiprocessing.html
#
########################################################################
# The MIT License (MIT)
#    http://opensource.org/licenses/MIT
#
#  Copyright (c) 2015 copyright cheungmine
#
# Permission is hereby granted, free of charge, to any person obtaining
#  a copy of this software and associated documentation files (the
#  "Software"), to deal in the Software without restriction, including
#  without limitation the rights to use, copy, modify, merge, publish,
#  distribute, sublicense, and/or sell copies of the Software, and to
#  permit persons to whom the Software is furnished to do so, subject
#  to the following conditions:
#
# The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
########################################################################

from multiprocessing.managers import BaseManager
from multiprocessing import RLock

from memdb import MemDB
from memdb import MemDBConnect

# global variables
#
lock = RLock()

memdbConn = MemDBConnect(MemDB())


class MemDBManager(BaseManager): pass


# thread safe class
#
class MemDBConnectProxy(object):
    global memdbConn

    def QueryTables(self):
        return memdbConn.QueryTablesNothrow()


    def QueryTableKeys(self, tablename):
        return memdbConn.QueryTableKeysNothrow(tablename)


    def CreateTable(self, tablename):
        memdbConn.CreateTableNothrow(tablename)
        pass


    def DropTable(self, tablename):
        memdbConn.DropTableNothrow(tablename)
        pass


    def GetValue(self, tablename, key, defaultvalue):
        return memdbConn.GetValueNothrow(tablename, key, defaultvalue)


    def AddValue(self, tablename, key, value):
        return memdbConn.AddValueNothrow(tablename, key, value)


    def DelValue(self, tablename, key):
        memdbConn.DelValueNothrow(tablename, key)


    def AppendValueList(self, tablename, key, value, non_repeated_value):
        memdbConn.AppendValueListNothrow(tablename, key, value, non_repeated_value)


    def AppendValueListMulti(self, tablenames, keys, values, non_repeated_values):
        memdbConn.AppendValueListMultiNothrow(tablenames, keys, values, non_repeated_values)


###########################################################
#
class MemDBManagerServer:
    MemDBManager.register(‘get_lock‘, callable=lambda: lock)
    MemDBManager.register(‘MemDBConnect‘, MemDBConnectProxy)

    def __init__(self, host, port, passkey):
        self.manager = MemDBManager(address=(host, port), authkey=passkey)
        self.server = self.manager.get_server()
        pass


    def start(self):
        self.server.serve_forever()
        pass


    def stop(self):
        self.server.shutdown()
        self.stop_flag = 1
        pass


###########################################################
# start(‘‘, 12345, ‘abc‘)
#
def start(host, port, passkey):
     from memdb_server import MemDBManagerServer

     server = MemDBManagerServer(host, port, passkey)
     server.start()

2 客户端代码:

#!/usr/bin/python
#-*- coding: UTF-8 -*-
# memdb_client.py
#
# Queues are thread and process safe.
#
########################################################################
# The MIT License (MIT)
#    http://opensource.org/licenses/MIT
#
#  Copyright (c) 2015 copyright cheungmine
#
# Permission is hereby granted, free of charge, to any person obtaining
#  a copy of this software and associated documentation files (the
#  "Software"), to deal in the Software without restriction, including
#  without limitation the rights to use, copy, modify, merge, publish,
#  distribute, sublicense, and/or sell copies of the Software, and to
#  permit persons to whom the Software is furnished to do so, subject
#  to the following conditions:
#
# The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
########################################################################

from multiprocessing.managers import BaseManager
from multiprocessing import RLock
from memdb import *


class MemDBConnectProxy(object): pass

class MemDBManager(BaseManager): pass


class MemDBManagerClient:
    MemDBManager.register(‘get_lock‘, callable=lambda: lock)
    MemDBManager.register(‘MemDBConnect‘, callable=lambda: MemDBConnectProxy)

    def __init__(self, servhost, servport, authkey):
        self.config = {
            "host": servhost,
            "port": int(servport),
            "authkey": authkey
        }
        self._lock = None
        self.manager = MemDBManager(address=(self.config[‘host‘], self.config[‘port‘]), authkey=self.config[‘authkey‘])
        self.manager.connect()
        pass


    def __del__(self):
        self.unlock()
        pass


    def lock(self):
        if not self._lock:
            self._lock = self.manager.get_lock()
            self._lock.acquire()
        pass


    def unlock(self):
        if self._lock:
            l = self._lock
            self._lock = None
            l.release()
        pass


    def getMemDBConnect(self):
        self.memdbConn = self.manager.MemDBConnect()
        return self.memdbConn


###########################################################
# start(‘‘, 12345, ‘abc‘)
#
def start(host, port, passkey):
    from memdb_client import MemDBManagerClient

    client = MemDBManagerClient(host, port, passkey)
    print "connect to memdb server success"
    memdbConn = client.getMemDBConnect()

    tables = ["china", "america", "russian"]

    # memdb_server.MemDBConnectProxy
    for t in tables:
        memdbConn.CreateTable(t)
    print memdbConn.QueryTables()

    memdbConn.AddValue("china", "capital", "Beijing")
    memdbConn.AddValue("china", "1-city", "Shanghai")
    memdbConn.AddValue("america", "capital", "Washington")
    memdbConn.AddValue("america", "1-city", "Newyork")
    memdbConn.AddValue("russian", "capital", "Moscow")
    memdbConn.AddValue("russian", "1-city", "Moscow")

    memdbConn.AppendValueListMulti(
        ["china", "america", "russian"],
        ["population", "population", "population"],
        [130000, 30000, 20000],
        [True, True, True])

    for t in tables:
        keys = memdbConn.QueryTableKeys(t)
        for k in keys:
            v = memdbConn.GetValue(t, k, None)
            print "%s: %r=>%r" % (t, k, v)

3 开二个终端,分别启动服务和客户端:

服务端:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import memdb_server
>>> memdb_server.start(‘‘, 12345, ‘abc‘)

客户端:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import memdb_client
>>> memdb_client.start(‘‘, 12345, ‘abc‘)
connect to memdb server success
[‘russian‘, ‘america‘, ‘china‘]
china: ‘1-city‘=>‘Shanghai‘
china: ‘population‘=>[130000]
china: ‘capital‘=>‘Beijing‘
america: ‘1-city‘=>‘Newyork‘
america: ‘population‘=>[30000]
america: ‘capital‘=>‘Washington‘
russian: ‘1-city‘=>‘Moscow‘
russian: ‘population‘=>[20000]
russian: ‘capital‘=>‘Moscow‘
>>> 




python 内存数据库与远程服务

标签:

原文地址:http://blog.csdn.net/ubuntu64fan/article/details/50425548

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!