Python-socket principle in network programming, pythonsocket

Source: Internet
Author: User

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.

 

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.