1. Talk about your understanding of multi-process, multi-threading, and coroutine. Is the project used?

  • The probability of this question was asked, in fact, multithreading, multi-process, rarely used in practical developing, unless particularly high performance requirements on the project, some development work for several years, is used, you can answer, so give him pull off what is process, thread (retaining is the concept of pseudo multithreaded), If not, mention that you’ve written about using multithreading to download files or in your spare time to write a crawler to increase productivity.
  • Process: a running program (code) is a process, no running code called program, process is the smallest unit of system resource allocation, process has its own independent memory space, so the data between processes do not share, the overhead is large.
  • Thread: The smallest unit of scheduled execution, also called the execution path, cannot exist independently, dependent on the process there is at least one thread, called the main thread, and multiple threads share memory (data sharing, sharing global variables), thus greatly improving the efficiency of the program.
  • Coroutine: is a user – mode lightweight thread, the scheduling of coroutine is completely controlled by the user. Coroutines have their own register context and stack. When the coroutine schedules the switch, the register context and stack are saved elsewhere, and when the switch is cut back, the previously saved register context and stack are restored. The direct operation of the stack basically has no kernel switching overhead, and you can access global variables without locking, so the context switch is very fast.

2. What is multi-threaded competition?

  • Threads are independent. Threads in the same process share data. When each thread accesses data resources, there will be a competition state, namely:

    • Nearly synchronous data can be occupied by multiple threads, resulting in data chaos, which is called thread insecurity
  • So how to solve the multithreaded competition problem? – the lock.

    • Benefits of locking: Ensuring that a critical piece of code (shared data resources) can only be executed from start to finish by a single thread solves the problem of atomic operations in multithreaded resource contention.
    • Disadvantages of locking:

      By preventing concurrent execution of multiple threads, a piece of code that contains a lock can actually only execute in single-threaded mode, and efficiency is greatly reduced

      • Deadly locks: deadlocks.

3. Explain what locks are and what kinds of locks are there?

Locks are objects that Python provides for thread control. There are mutexes, reentrant locks, deadlocks.

4. What is a deadlock?

  • When several sub-threads compete for system resources, they are all waiting for each other to release the occupied state of some resources. As a result, no one is willing to unlock first and the program cannot be executed. This is deadlock.
  • GIL lock (only available in CPython) : the global interpreter lock (only available in CPython) is used to restrict simultaneous execution of multiple threads, so that only one thread can execute at the same time. So in Python, coroutines are often used to replace multithreading. Coroutines are more lightweight threads. Process and thread switching are determined by the system, while coroutines are determined by our programmers.
  • There are threads in the process and coroutines in the thread.

5. How are processes and threads used in Python?

  • Multiple processes are suitable for CPU-intensive operations (such as floating-point operations).
  • Multithreading is suitable for IO intensive operations (reading and writing data more operations, such as crawlers).

6. Are threads concurrent or parallel, and processes concurrent or parallel?

Threads are concurrent, processes are parallel; Processes are independent of each other and are the smallest unit of resources allocated by the system. All threads in the same thread share resources.

7. Concurrency and parallel?

Parallelism: Multiple tasks running at the same time. Concurrency: Multiple tasks are running at the same time interval, but not at the same time.

Scan code to focus on not getting lost

Focus on sharing and communicating Java price technology