"Python Road 33" Development Mode Singleton mode

Source: Internet
Author: User

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 access
Web Server Listener code

"Python Road 33" Development Mode Singleton mode

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.