python rear face questions -web

1、.socket,udp,tcp

1.1-socket

socket (socket abbreviation) is a tool for inter-process communication, it can achieve the data transmission from one party to the other party, the communication between processes on different computers, it is like porter data.

1.2-UDP

UDP is a transport layer protocol for simple datagram.

  • advantage

    • Transmission speed (since UDP established between client and server prior to transmission packet data without a connection, there is no timeout and retransmission mechanisms)

    • No connection, small resource overhead

  • Shortcoming

    • Data transmission is not reliable, easy to lose packets

    • No flow control, when the other person did not receive the data, the sender has been sending data can result in data buffer is full, the computer appears stuck, all the recipient needs to receive timely data.

  • scenes to be used

    When the network communication quality less demanding, requiring network communication speed can be fast as possible, then you can use UDP

    • qq audio and video transmission, micro-channel audio and video transmission

    • Send a broadcast message

1.3-TCP

TCP: English spelling (Transmission Control Protocol) transport control protocol for short, which is a connection-oriented, reliable transport layer protocol is byte-oriented.

  • advantage

    • Reliable, stable

    • Suitable for transmitting large amounts of data

  • Shortcoming

    • Transmission speed

    • High system resources

  • scenes to be used

    • Browser

    • QQ file transfer

  • The difference between TCP and UDP

    1. TCP connection-oriented; the UDP is not connection-oriented;

    2. TCP provides reliable data transmission, i.e., data transmitted through the TCP connection, error-free, is not lost, will not be repeated, and arrive out; the UDP does not guarantee reliable data transmission, packet loss situation prone;

    3. TCP connection needs to slow transmission, UDP does not require connection, transmission speed

    4. TCP does not support broadcasting hair; UDP broadcast support hair

    5. TCP more demanding on system resources, UDP less demanding on system resources.

    6. TCP for sending large amounts of data, UDP for sending small amounts of data

    7. TCP flow control, UDP is no flow control

2, processes, coroutines, thread

  1. Feature Comparison

    • Process, to complete multiple tasks, such as the ability to run multiple QQ simultaneously on a single computer

    • Threads, to complete multiple tasks, such as multiple chat windows in a QQ

  2. Defined comparison

    • Process is the basic unit of resource allocation systems, each operating system to start a process needs to assign resources to run.

    • A thread is an executive branch run the program, is the basic unit of CPU scheduling. (Operating system process is the basic unit of resource allocation, thread is a basic unit of CPU scheduling)

    • Coroutine: complete multiple tasks on the basis of not opening up the thread, that is, to complete multiple tasks in a single-threaded case, alternately perform multiple tasks in a certain order

  3. Contrast relationship

    • A thread is attached to the inside of the process, no process is no thread

    • A process to provide a default threads, processes can create multiple threads

    • There are a plurality of thread can coroutine

  4. the difference

    • Global variables are not shared between processes

    • Shared global variables, but be aware that competition for resources between threads of the problem, the solution: mutex or thread synchronization

    • The process of creating resource overhead than creating a thread to a large resource overhead

    • Process operating system is the basic unit of resource allocation, the thread is the basic unit of CPU scheduling

    • Thread can not execute independently, they must exist in accordance with the process

    • Multi-process development is stronger than a single multi-threaded development process stability

    • Multi-process, multi-thread based on the number of cpu core is not as likely to be parallel, but coroutines is in a thread so that concurrent

  5. Advantages and disadvantages

    • multi-Progress:

      • Advantage: multicore

      • Disadvantages: large resource overhead

    • Multithreading:

      • Advantages: small resource overhead

      • Disadvantages: not multicore

    • Coroutine:

      Coroutine task switching resources is small, high efficiency

3 applications, multi-threading in the web project

Multi-threaded general use during IO operations

1, such as business logic requires a concurrent read and write operations of several files have to be performed synchronously or asynchronously not performed, this time through multiple threads can read these files

2, view the need to request multiple third-party interfaces, also still requires synchronous, asynchronous can not, at this time you can also use multi-threaded parallel requests to multiple third-party interfaces

This operation 3, for example, in orders in the system, it is necessary to modify the product after the order is submitted inventory, sales and other merchandise

4, Python2 difference in the range and xrange?

Both use the same,

The difference is the result of range returns a list,

Xrange the result is a generator,

The former is to open up a direct memory space to save the list,

While circulating the latter is used only when the memory space will be open to use,

So when the list is long, use xrange range performance than good.

5, shallow copy deep copy

img

6、celery

celery is based on an asynchronous task scheduling tool implemented in Python, is also a task queue, mainly for handling time-consuming task.

  • principle:

    Celery is composed of three parts, the middleware message (message broker), the task execution unit (worker) and task execution result storage (task result store) composition.

    • Middleware message (message broker): task scheduling queue, the service is an independent producer task into the queue, the task execution worker removed from the task queue, Celery message service does not provide, so to integrate third party queue It recommended RabbitMQ, Redis as a message queue.

    • Task execution means (worker): real-time monitoring its message queue, the acquired queue scheduling tasks and performing

    • Task results are stored (task result store): Worker used to store task execution results, Celery support results in different ways to store tasks, including AMQP, Redis, memcached, MongoDB, SQLAlchemy, Django ORM, Apache Cassandra, IronCache

  • Why use Message Queue

    In a highly concurrent environment, since the time to process the same time, requests often clogged, by using message queues, we can process the request asynchronously, to relieve pressure of the system

  • scenes to be used

    Asynchronous task processing: for example, sending a short message or a confirmation message to the registered user task. Large task: Perform a long time tasks, such as video and image processing, add watermark and transcoding, you need to perform long-time task. The timing of execution of the tasks: support task execution and timing set time execution. For example when performing pressure measurement performance.

  • Shortcoming

    Reduce system availability, improve system complexity, consistency

  •  

Guess you like

Origin www.cnblogs.com/yblackd/p/12341064.html