首页 > 编程 > Python > 正文

python高并发异步服务器核心库forkcore使用方法

2020-02-23 04:58:13
字体:
来源:转载
供稿:网友

1 拷贝下面的代码到一个文件,并命名为forkcore.py

代码如下:
import os
import threading
import select
import socket

class ds_forkcore(object):

    #async IO(epoll)
    def ds_epoll(self):
        epoll=select.epoll()
        epoll.register(self.s.fileno(),select.EPOLLIN|select.EPOLLET)
        while 1:
            epoll_list=epoll.poll()
            for fd,_events in epoll_list:
                if fd==self.s.fileno():
                    conn,addr=self.s.accept()
                    print "Current process's pid is "+str(os.getpid())
                    self.worker(conn,addr)

    #multi_thread
    def ds_thread(self,thread_num=100):
        for _ in range(0,thread_num):
            t=threading.Thread(target=self.ds_epoll)
            t.setDaemon(1)
            t.start()
            t.join()

    #multi_process
    def ds_process(self,child_process_num=8):
        pid=os.getpid()
        print "Main process start, pid is "+str(pid)
        for _ in range(0,child_process_num):
            if pid==os.getpid():
                if os.fork():
                    pass
                else:
                    print "Worker process start, pid is "+str(os.getpid())
                    self.ds_thread()

    #init function
    def __init__(self,worker,port=3333):
        s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表