The thread pool and the Gil in Python

Source: Internet
Author: User

The thread pool is the concept of an operating system, which is an optimization of multithreading.

When multithreading, creating and destroying threads is associated with the overhead of the operating system, and if threads are created/destroyed frequently, the efficiency is greatly reduced.

The thread pool is the first to create a batch of threads into the pool, need to create a thread from the pool, and then put it in the pool.

Obviously, the use of the thread pool saves time for threads to be created and destroyed.

This is a space-for- time approach because the created thread is to be placed somewhere in memory.

Another benefit of using the thread pool is that it limits the number of threads.

How many threads in the thread pool can be specified manually.

Joining does not use a thread pool, so if you create a lot of threads at once, scheduling between threads can waste a lot of time.

The thread pool limits the number of threads in multithreaded situations, and improves execution efficiency by reducing multi-threading.

In Python, because there is a Gil(Global interpreter Lock) lock, the Gil Lock is first obtained before any Python thread executes.

Each execution of 100 bytecode, the interpreter automatically releases the Gil lock, so that other threads have the opportunity to execute on the same CPU.

So on the surface, because of the Gil's existence, the multithreading in Python seems to have little effect.

It's actually a bit of a function, for IO-intensive tasks, when a long wait time is required, the Gil is conceded so that other threads can get

Gil and run.

The thread pool and the Gil in Python

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.