python高并发异步服务器核心库forkcore使用方法
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)
s.bind(("",port))
s.listen(50000)
self.s=s
self.worker=worker
self.ds_process()
2 编写自己的代码
1> 导入forkcore库。
2> 定义worker函数,worker函数需要两个参数,conn代表客户端连接的socket,addr是(ip,port)的元组。
3> 直接使用forkcore.ds_forecore(worker,port=5555)即可,port用于指定监听端口。
import forkcore
if __name__=="__main__":
def worker(conn,addr):
print "Message from ("+str(addr[0])+":"+str(addr[1])+"): "+conn.recv(1024)[0:-1]
forkcore.ds_forkcore(worker,port=5555)
注:需要linux 2.6以上的内核

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...
