Problem 1: PythonmultithreadingAs well asMultiple processesHow to pass parameters in?

Python multithreading has a global interpreter lock, or GIL. The GIL is not a Python feature. It is a concept introduced only in Cpython interpreters, and is not found in other interpreters. Jython.

This lock means that only one thread can run the interpreter at any one time. It is the same as running multiple programs on a single CPU. We use it in rotation.

Why is there GIL?

The emergence of multi-core CPU, make full use of multi-core, using multi-thread programming gradually popularized, the difficulty is the consistency of data and state synchronization between threads

Said GIL interpreter lock, we easily think of in a multi-threaded Shared global variables will have a thread to the resource competition of global variables, the global variable changes is not what we want results, and at that time we used is the inside of the threading module in python mutex, which can every time operating on a global variable, Only one thread can get hold of the global variable; Look at the following code:

import threading
global_num = 0


def test1(a):
    global global_num
    for i in range(1000000):
        global_num += 1

    print("test1", global_num)


def test2(a):
    global global_num
    for i in range(1000000):
        global_num += 1

    print("test2", global_num)

t1 = threading.Thread(target=test1)
t2 = threading.Thread(target=test2)
t1.start()
t2.start()

Copy the code

Next, add the mutex

import threading
import time
global_num = 0

lock = threading.Lock()

def test1(a):
    global global_num
    lock.acquire()
    for i in range(1000000):
        global_num += 1
    lock.release()
    print("test1", global_num)


def test2(a):
    global global_num
    lock.acquire()
    for i in range(1000000):
        global_num += 1
    lock.release()
    print("test2". global_num) t1 = threading.Thread(target=test1) t2 = threading.Thread(target=test2) start_time = time.time() t1.start() t2.start()Copy the code

When is it appropriate to use multithreading?

As long as you can free the GIL during time-consuming IO operations, multithreading is appropriate in IO intensive code

When are multiple processes appropriate?

Computationally intensive, such as calculating the size of a folder

Data sharing between multiple processes

Share data between multiple processes using multiprocession.Value and multiprocessing.Array

2. Explain python’s AND-OR syntax

bool and a or b

Equivalent to a bool? a: b

>>> a = "first"
>>> b = "second"
>>> 1 and a or b  # output 'first'
>>> 0 and a or b  # output 'second'
Copy the code

This should make sense to you, but there is a problem. Look at the code below

>>> a = ""
>>> b = "second"
>>> 1 and a or b # output 'second'

Copy the code

Because a is an empty string, which is considered false by Python in a Boolean environment, this expression will “fail” and return the value of B. If you don’t think of it like bool, right? A: THE same syntax as b, but consider it pure Boolean logic, then it will be understood correctly. 1 is true, a is false, so 1 and a is false. False or B is B.

The and-or technique should be encapsulated as a function:

def choose(bool, a, b):
    return (bool and [a] or [b])[0]
Copy the code

Because [a] is a non-empty list, it can never be false. Even if a is 0 or “or some other false value, the list [a] is true because it has one element.

Question 3: Please list at least 5 PEP8 specifications?

PEP8 specification official documentation: www.python.org/dev/peps/pe… PEP8 Chinese translation: www.cnblogs.com/ajianbeyour…

This depends on the accumulation of peacetime

  • The indentation. 4 Spaces indent (any editor can do this), no Tap, and no mix of Tap and Spaces.
  • Each line has a maximum length of 79. You can use backslashes for line breaks, and parentheses are preferred. A newline point is followed by a return type.
  • Empty two lines between the class and the top-level function definition; An empty line between method definitions in a class; A blank line between logically irrelevant paragraphs in a function; Try to avoid blank lines elsewhere.
  • Block comment, a comment added before a piece of code. Add a space after the ‘#’. Paragraphs are separated by only ‘#’ lines
  • Do not precede the various closing parentheses with Spaces.
  • Do not place Spaces before commas, colons, and semicolons.
  • Do not precede the opening parenthesis of a function with a space.
  • Do not precede the opening parenthesis of a sequence with a space.
  • Add a space to the left and right of the operator. Do not add Spaces for alignment.
  • Function default arguments use assignment operators that omit Spaces left and right.
  • Do not write multiple statements on the same line, although use ‘; ‘permission. An i-f /for/while statement must start on another line, even if the statement is executed on only one line.
  • Class methods must take self as their first argument, while static methods must take CLS as their first argument.

The difference between HTTPS and HTTP:

  • HTTPS requires a certain economic cost to apply for a certificate to a CA
  • HTTP is plaintext transmission and HTTPS is encrypted secure transmission
  • The connection port is different. HTTP is 80 and HTTPS is 443
  • HTTP connections are simple and stateful; HTTPS is a network protocol used for identity authentication. It is more secure than HTTP.

There are many more. Go and sort them out yourself

Djangos ORM

ORM stands for object-relation Mapping

Decoupling the data model from the database, easily changing the database through simple configuration, without changing the code just object-oriented programming, ORM operations are essentially based on the connected database engine, All projects developed using Django don’t need to worry about MySql, Oracle, or SQLite…. In case of database migration, just change Djangos database engine.

Question 6: Concern, recently updated a magical series of introductory articles on the public account

At the age of 27, she learned C, C ++ and Python programming languages from scratch. At the age of 29, she wrote 100 tutorials. At the age of 30, she mastered 10 programming languages

Welcome to follow her public account, search – non-undergraduate programmer

The serial number The article
Python interview question No1 See this article to highlight the basics of Python interviewing
Interview questions No2 Ask questions quickly in a Python interview. The theory is speed and precision
Python interview question No3 Learning Python for a year, I forgot the basics, look at the interview questions to recall
Interview questions No4 When interviewing Python engineers, you must be able to answer these basic questions
Interview questions No5 6 interview questions Python Engineers must Take in 2019
Interview questions No6 I went to the interview yesterday and got all 5 Python interview questions
Interview questions No7 On Wednesday, I will be interviewing you for Python development. I will be interviewing you for Python development
Interview questions No8 When interviewing Python engineers, you will need to memorize these coding questions
Interview questions No9 I interviewed Python reptile engineers again, and came across several interview questions
Interview questions No10 My friend went to interview Python engineers and came back with some basic questions
Interview questions No11 The questions were too difficult for a recent graduate interview with a Python engineer
Interview questions No12 I memorized these Python interview questions in 54 days
Interview questions No13 I didn’t get these Python interview questions right
Interview questions No14 Check out these python interview questions for your job search tomorrow
Interview questions No15 Here are some Python interview questions