Karrigell_multiprocess.py :  » Web-Frameworks » Karrigell » Karrigell-3.1 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Karrigell 
Karrigell » Karrigell 3.1 » Karrigell_multiprocess.py
"""Multi process version of the Async Server
Requires the PyProcessing package
Adapted from a code written by ccnusjy on
http://groups.google.com/group/karrigell/browse_frm/thread/c2c949b04e0f95cd
"""

import os
import traceback
import sys

from multiprocessing import Process,freeze_support,RLock


if sys.platform == 'win32':
    import multiprocessing.reduction    # make sockets pickable/inheritable


def kkk_serve(server_config, use_ipv6):
    try:
        import k_config
        k_config.init(server_config)

        from Async_core import Server,handler

        s = Server(('', k_config.port), handler, use_ipv6)
        s.run()
    except KeyboardInterrupt:
        s.close()
        sys.stderr.write("Ctrl+C pressed. Shutting down.\n")


def main_start(server_config):
    try :
        # Launch all ipv4 child processes
        childs = []
        for i in range(server_config["process_num"]):
            child = Process(target=kkk_serve, args=(server_config,False))
            child.daemon= True
            child.start()
            childs.append(child)
            #import time
            #time.sleep(0.5) # To see debug messages correctly.
            
        if server_config["use_ipv6"] == True :
            # Launch all ipv6 child processes
            for i in range(server_config["process_num_ipv6"]):
                child = Process(target=kkk_serve, args=(server_config,True))
                child.daemon= True
                child.start()
                childs.append(child)
                #import time
                #time.sleep(0.5) # To see debug messages correctly.
    
        import k_version
        print "Karrigell %s running on port %s\n%s processes using ipv4" \
            %(k_version.__version__, server_config["port"], server_config["process_num"])
        if server_config["use_ipv6"] == True :
            print "%s processes using ipv6" % server_config["process_num_ipv6"]
        else :
            print ""
            
        print "Press Ctrl+C to stop"
    
        child.join()
    except KeyboardInterrupt :
        sys.stderr.write("Ctrl+C pressed. Shutting down.\n")
    
    # multiprocessing doc recommends to join all child processes on a unix system
    for c in childs:
        c.join()

if __name__ == '__main__':
    if sys.platform == 'win32':
        freeze_support()

    # read command line and load configuration
    import config_loader
    server_config = config_loader.load()
    server_config["server_type"] = "Multiprocess"
    server_config["rlock"] = RLock()
        
    main_start(server_config)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.