Threads thread thread notes for Python

Source: Internet
Author: User

Python threads are implemented using thread and threading. It is better to take advantage of threading because thread is not protected by threads, and when the main thread exits, the child threads are forced to quit. The threading supports the daemon thread.

The common method in thread: Thread.allocate_lock () is to return a new locked object

Acquire ()/release () is the two states of the original lock, locking and unlocking, which is the operation of the locked object established above.

Thread.start_new_thread (func, parameter of func) is the creation of a new thread, where the first two parameters must be filled, and if the function does not require arguments, pass in an empty tuple.

Use of threading:

Create a thread:

Threading. Thread (target = Youfunc,args = (your func ' s args))

    

Start ()

Start thread activity

Join ()

Waiting for thread to terminate

After all the threads have been created, call the start () function together to start, instead of creating a startup one. And, instead of managing a bunch of locks (assigning locks, acquiring locks, releasing locks, checking the state of locks, etc.), simply call the join () function on each thread.

Join () waits until the thread ends, or when the timeout parameter is given, until the timeout is reached. Another important aspect of join () is that it can not be called at all. Once the thread is started, it runs until the thread's function ends and exits.

Excerpt from the Insect Guru's blog.

Threads thread thread notes for 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.