Python Library learning Notes (threading Library)

Source: Internet
Author: User
Tags thread class

The Import threadingthreading module providesclass。

1. Thread: An object that represents the execution of a thread.

2. Lock: Locking Primitive Object

3. Rlock: The lock object can be re-entered. Enables a single thread to get the lock already acquired again.

4. Condition: Condition Variable object. Allows a thread to stop waiting for another thread to meet the "one" condition.

5. Event: a generic condition variable. Multiple threads can wait for a certain time to occur, and all threads are activated after the event occurs.

6. Semaphore: Provides a "waiting room" structure for the thread waiting for the lock

7. Timer: Similar to thread, except that it waits some time before it starts to run.

Threading'sThread ClassIn the method.

1. Thread.Start (): Start thread execution.

2. thread.join(timeout=none): Blocks the main program until the thread ends and if timeout is given, it blocks timeout seconds.

3. Thread.run (): Defines the functionality of a thread (typically overridden by a quilt class)

4. Thread.getname (): Returns the name of the thread.

5. Thread.setname (): Sets the name of the thread.

6. Thread.isalive (): Checks if the thread is still executing.

7. Thread.isdaemon (): see if the thread is a daemon thread.

8. Thread.setdaemon (): True to set the daemon thread , false is not, be sure to call before the start () method.

Instance:
1 #-*-Coding:utf-82 ImportThreading3  fromTimeImportCTime, Sleep4 5 defMusic (func):6      forIinchRange (2):7         Print "I was listening to%s.%s"%(func, CTime ())8Sleep (1)9 Ten defMovie (func): One      forIinchRange (2): A         Print "I was watching the%s.%s"%(func, CTime ()) -Sleep (5) -  the #Start Creating Threads -Threads = [] -T1 = Threading. Thread (Target=music, args= (U'Love Business',)) - threads.append (t1) +T2 = Threading. Thread (Target=movie, args= (U' Avatar',)) - threads.append (T2) +  A  at if __name__=='__main__': -      forTinchThreads: -T.start ()#Running Threads -T.join ()#blocks the main process until the second thread ends.  -     PrintU' Over', CTime ()

If the 18th line is stacked behind the 20th line of code, a different result will appear. Because T.join () in the 26th line of code can only guarantee the end of the last thread in the loop, it is possible to set a join () method for each thread.

1  for inch Threads: 2     T.join
Reference blog:

Https://www.cnblogs.com/fnng/p/3670789.html

50155353

Python Library learning Notes (threading Library)

Related Article

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.