1, the singleton mode refers to the creation of a single instance, for example: The database connection pool contains 10 database connections, the user accesses the data randomly from the connection pool to take one to connect, other users no longer access to create objects to connect
#!usr/bin/env python#-*-coding:utf-8-*-classConnecttionpool:__instance=Nonedef __init__(self): Self.ip='222.133.177.67'Self.port='3306'Self.username='Odoo'Self.password='123456'self.con_list= [1,2,3,4,5,6,7,8,9,10] @staticmethoddefGet_instance ():#methods for creating instances ifConnecttionpool.__instance: returnConnecttionpool.__instance Else: Connecttionpool.__instance=Connecttionpool ()returnConnecttionpool.__instance defget_connection (self):#Get Connected ImportRandom R= Self.con_list[random.randrange (1,10)] returnR forIinchRange (10): Pool=connecttionpool.get_instance ()Print('go to Connection pool', Pool,'Get a connection') R=pool.get_connection ()Print('the connections obtained are:', R)single-case mode
2. Simple Server Request Framework
#!usr/bin/env python#-*-coding:utf-8-*- fromWsgiref.simple_serverImportMake_serverdefRunserver (environ,start_response): Start_response (Status='OK', headers=[('Content-type','text/html')]) URL= environ['Path_info']#URL takes the address after the port number that is accessed return 'Thank you for your visit! ' #can read HTML filesif __name__=='__main__': httpd= Make_server ("', 8000, Runserver)Print('Server Http on 8000 ...') Httpd.serve_forever ()#Loop Listener Access, returns the return value of the Runserver function when there is accessWeb Server Listener code
"Python Road 33" Development Mode Singleton mode