Python Memory database and remote service

Source: Internet
Author: User

Python Memory database and remote service


The Python Memory database code that requires import refers to the following link:

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


Now the question:

Create a Memory data service that can be connected to local or remote users, concurrently read and write by multiple users concurrently

This memory database.

There are 3 steps to resolve:

(1) Create server (memdb_server.py)

(2) Create client (memdb_client.py)

(3) Start the server and start the client

1 server code:

#!/usr/bin/python #-*-coding:utf-8-*-# memdb_server.py # # Queues is thread and process safe. # Refer: # https://docs.python.org/2/library/multiprocessing.html # ############################################### ######################### # The MIT License (MIT) # Http://opensource.org/licenses/MIT # # Copyright (c) copyrigh T Cheungmine # Permission is hereby granted, free of charge, to all person obtaining # A copy of this software and Asso ciated documentation Files (the # "software"), to deal in the software without restriction, including # without Limitati
On 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 are furnished to doing so, subject # to the following conditions: # # The above Copyri
Ght Notice and this permission notice shall is # included in all copies or substantial portions of the software. # # 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 P
Urpose and Noninfringement. # in NO EVENT shall the AUTHORS or COPYRIGHT holders is 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 DEA
Lings in the software. ######################################################################## from multiprocessing.managers Import
Basemanager from multiprocessing import Rlock to 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, Defaultval UE): Return Memdbconn.getvaluenothrow (tablename, Key, DefaultValue) def addvalue (self, tablename, key, value ): Return Memdbconn.addvaluenothrow (tablename, key, value) def delvalue (self, tablename, key): Memdb Conn.delvaluenothrow (tablename, key) def appendvaluelist (self, tablename, key, Value, Non_repeated_value): M Emdbconn.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.regis ter (' 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.server.serve_forever () Pass def Stop (self): self.se Rver.shutdown () Self.stop_flag = 1 Pass ########################################################### # St Art (", 12345, ' abc ') # Def start (host, Port, passkey): From memdb_server import memdbmanagerserver server = Me
 Mdbmanagerserver (host, Port, passkey) Server.start ()

2 Client code:

#!/usr/bin/python #-*-coding:utf-8-*-# memdb_client.py # # Queues is thread and process safe. # ######################################################################## # The MIT License (MIT) # Http://opensource . Org/licenses/mit # # Copyright (c) Copyright Cheungmine # # Permission is hereby granted, free of charge, to any PE Rson 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, Subli  Cense, and/or sell copies of the software, and to # Permit persons to whom the software are furnished to doing so, subject #  To the following conditions: # # of above copyright notice and this permission notice shall is # 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 and not LIMITED to the WarraNties of # merchantability, FITNESS for A particular PURPOSE and noninfringement. # in NO EVENT shall the AUTHORS or COPYRIGHT holders is 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 DEA
Lings in the software. ######################################################################## from multiprocessing.managers Import Basemanager from multiprocessing import rlock from memdb Import * class Memdbconnectproxy (object): Pass class Memdbmana GER (Basemanager): Pass class MemDBManagerClient:MemDBManager.register (' Get_lock ', Callable=lambda:lock) memdbm
        Anager.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 s Elf._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 () pa SS 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 M Emdb server Success "Memdbconn = Client.getmemdbconnect () tables = [" China "," America "," Russian "] # memdb_s Erver.
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 "," W
    Ashington ") Memdbconn.addvalue (" America "," 1-city "," NewYork ") Memdbconn.addvalue (" Russian "," capital "," Moscow ") Memdbconn.addvalue ("Russian", "1-city", "Moscow") Memdbconn.appendvaluelistmulti (["China", "America", "R

    Ussian "], [" 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 Open two terminals, start the service and the client separately:

Service side:

Python 2.7.6 (Default, June, 17:58:13) 
[GCC 4.8.2] on linux2
Type ' help ', ' copyright ', ' credits ' or ' Licens E "For more information.
>>> import memdb_server
>>> memdb_server.start (", 12345, ' abc ')

Client:

Python 2.7.6 (Default, June, 17:58:13) 
[GCC 4.8.2] on linux2
Type ' help ', ' copyright ', ' credits ' or ' Licens E "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 Memory database and remote service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.