Python-socket principle in network programming, pythonsocket
Python is a powerful network programming tool. We start from the socket module here.
Methods for handling multiple connections: forking, threading, asynchronous I/O
Forking: There is a parent process listening for client connection. When there is a connection, copy a child process to process the transaction with the client, after the client request ends, the sub-process exits. Forking is resource-consuming, and each sub-process has its own memory space. forking is not supported in windows.
Threading (thread): The thread exists in the same city and shares the memory, but there is a synchronization problem. Pay attention to locking.
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 import threading 4 import time 5 6 def test (num): 7 time. sleep (1) 8 print 'this is % s' % num 9 10 for I in range (10): 11 t = threading. thread (target = test, args = (I,) 12 t. start ()
Asynchronous I/O: asynchronous I/O. The mechanism is the select function of the select module.