python study notes (below)

1. Explain the performance optimization of web projects from the front-end, back-end and database respectively

Front-end optimization:

1. Reduce http requests, such as making sprite images

2. HTML and CSS are placed on the upper part of the page, and javascript is placed below the page. Because js is slower to load than HTML and Css, html and css should be loaded first to prevent incomplete page display, poor performance, and poor user experience.

Back-end optimization:

1. The cache stores data with high read and write times and few changes, such as the information on the homepage of the website and the information on the products. When an application reads data, it usually reads it from the cache first. If it cannot be read or the data has expired, it accesses the disk database and writes the data to the cache again.

2. Asynchronous mode, if there is a time-consuming operation, you can use asynchronous, such as celery

3. Code optimization to avoid too many cycles and judgments. If there are multiple if else judgments, judge the most likely situation first

Database optimization:

1. If possible, the data can be stored in redis with fast reading speed

2. Create indexes, foreign keys, etc.

2. List common MYSQL data storage engines

1. InnoDB: supports transaction processing, foreign keys, crash repair capabilities and concurrency control. If you need to have relatively high requirements for transaction integrity (such as banks) and concurrency control (such as ticket sales), choosing InnoDB has great advantages. If you need to update and delete the database frequently, you can also choose InnoDB, because it supports the commit and rollback of transactions.

2. MyISAM: Insert data quickly, and use less space and memory. If the table is mainly used to insert new records and read out records, then choosing MyISAM can achieve high processing efficiency. It can also be used if the application integrity and concurrency requirements are relatively low.

3. MEMORY: All the data is in the memory, the data processing speed is fast, but the security is not high. If you need a fast read and write speed and have low data security requirements, you can choose MEMOEY. It has requirements on the size of the table and cannot create a table that is too large. Therefore, this type of database is only used in relatively small database tables.

2. Briefly describe the same-origin strategy

The same-origin strategy needs to meet the following three requirements at the same time:

1) Same agreement

2) Same domain name

3) Same ports

http:www.test.com and https:www.test.com are different sources-different protocols

http:www.test.com and http:www.admin.com are different sources-different domain names

http:www.test.com and http:www.test.com:8081 are different sources-different ports

As long as any one of these requirements is not met, the same-origin policy is not met, and "cross-domain" will appear

3. Use two methods to remove spaces

>>> a = 'welcome to MuMu`s Home page'
>>> b = a.replace(' ','')
>>> b
'welcometoMuMu`sHomepage'
>>> c = a.split(' ')
>>> c
['welcome', 'to', 'MuMu`s', 'Home', 'page']
>>> d = ''.join(c)
>>> d
'welcometoMuMu`sHomepage'

4. Int usage

>>> int('1.4')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.4'
>>> int(4)
4
>>> int('4')
4

5. Briefly describe optimistic locking and pessimistic locking

Pessimistic locking is very pessimistic. Every time you get the data, you think that someone else will modify it, so every time you get the data, you will lock it, so that if someone wants to get the data, it will block until it gets the lock. Many such locking mechanisms are used in traditional relational databases, such as row locks, table locks, etc., read locks, write locks, etc., which are all locked before operations.

Optimistic locking is very optimistic. Every time I get the data, I think that others will not modify it, so it will not be locked. However, when updating, it will judge whether others have updated the data during this period. You can use the version number. And other mechanisms, optimistic locking is suitable for multi-read application types, which can improve throughput

6. Linux command redirection> and >>

Linux allows the command execution result to be redirected to a file

Output/append the content that should be displayed on the terminal to the specified file

> Indicates output, which will overwrite the original content of the file

>> means append, the content will be appended to the end of the existing file

Example usage:

Save the information output by echo to 1.txt echo Hello Python> 1.txt

Append the information output by tree to the end of the 1.txt file tree >> 1.txt

7. Briefly describe optimistic locking and pessimistic locking

Function parameters in Python are passed by reference (note that they are not passed by value). For immutable types (numerical type, string, tuple), the dependent variable cannot be modified, so the operation will not affect the variable itself; for variable type (list dictionary), the function body operation may change the incoming Parametric variables.

8. Find the intersection, difference, and union of two lists

>>> a=[1,2,3,4]
>>> b=[4,3,5,6]
>>> jj1=[i for i in a if i in b]
>>> jj1
交集:[3, 4]
>>> jj2=list(set(a).intersection(set(b)))
>>> jj2
交集:[3, 4]
>>> bj=list(set(a).union(set(b)))
>>> bj
并集:[1, 2, 3, 4, 5, 6]
>>> cj1=list(set(a).difference(set(b)))
>>> cj1
差集(相对于a):[1, 2]
>>> cj2=list(set(b).difference(set(a)))
>>> cj2
差集(相对于b):[5, 6]

9. Generate random numbers from 0-100

>>> import random
>>> a=100*random.random()
>>> a
随机小数:47.56220877337418
>>> b=random.choice(range(1,100))
>>> b
随机整数:72
>>> c=random.randint(1,100)
>>> c
随机整数:53

10. Single quote, double quote, triple quote usage

1. There is no difference between single quotation mark and double quotation mark, but single quotation mark does not need to press shift, typing is slightly faster. When expressing a string, double quotation marks can be used in single quotation marks instead of escape characters, and vice versa.

'She said:"Yes." ' 或 "She said: 'Yes.' "

2. But if you use single quotation marks to expand single quotation marks directly, you need to escape them, like this:

' She said:\'Yes.\' '

3. Three quotation marks can directly write multiple lines, usually used for large paragraphs, large-length strings

"""
hello
world
"""

11. The difference between get and post in HTTP requests

1. A GET request directly requests data through a URL, and the data information can be seen directly in the URL, such as browser access; while a POST request is placed in the request header, we cannot see it directly;

2. GET submission has a limit on the data size, generally no more than 1024 bytes, and this statement is not completely accurate. The HTTP protocol does not set the upper limit of the URL byte length, but the browser does some processing. Therefore, the length varies depending on the browser; the POST request is not explained in the HTTP protocol. Generally speaking, there is no set limit, but in fact, the browser also has a default value. In general, a small amount of data uses GET, and a large amount of data uses POST.

3. Because the data parameters of the GET request are exposed in the URL, the security is relatively low. For example, the password cannot be exposed, so the GET request cannot be used; in the POST request, the request parameter information is placed in the request header, so the security Higher and can be used. In practice, when it comes to login operations, try to use HTTPS requests for better security.

12. Briefly describe multi-threading and multi-process

process:

1. The basic unit of the operating system for resource allocation and scheduling, and multiple processes are independent of each other

2. Good stability, if a process crashes, it will not affect other processes, but the process consumes a lot of resources, and the number of open processes is limited

Thread:

1. The basic unit of CPU resource allocation and scheduling. A thread is a part of a process and a basic unit that is smaller than a process and can run independently. Multiple threads in a process can share all the resources of the process.

2. If the IO operation is intensive, it can run in multiple threads with high efficiency. The disadvantage is that if a thread crashes, it will cause the process to crash

application:

IO-intensive use of multi-threading, during user input, sleep, you can switch to other threads for execution, reducing waiting time

CPU intensive use of multiple processes, because if there are few IO operations and multithreading, because the threads share a global interpreter lock, the currently running thread will occupy the GIL, and other threads without the GIL can not take full advantage of the multi-core CPU.

Guess you like

Origin blog.csdn.net/Lin_Hv/article/details/105792377