Python Threading Basic Learning

Source: Internet
Author: User

#-*-Coding:utf-8-*-

# python:2.x

__author__ = ' Administrator '

"""

Python is a thread that supports multithreading and is native. This is achieved mainly through the two modules of thread and threading. Thread is a relatively low-level module, threading is the thread to do some packaging, can be more convenient to be used. What you need to mention here is that Python's support for threading is not perfect enough to take advantage of multiple CPUs, but the next version of Python has already been considered to improve this, let's wait and see.

Threading module is mainly for the operation of some threads of the object, the creation of a class called Thread. In general, there are two modes of using threads, one is to create a function to execute the thread, pass the function into the thread object, let it execute, and the other is to inherit from thread directly, create a new class, and put the code executed by the thread into the new class. Let's take a look at both of these approaches.

"""

#例子1

Import string, threading, Time

Def Thread_main (a):

Global count, Mutex

# Get Thread Name

ThreadName = Threading.currentthread (). GetName ()

For x in xrange (0, Int (a)):

# Get the Lock

Mutex.acquire ()

Count = Count + 1

# release Lock

Mutex.release ()

Print ThreadName, X, Count

Time.sleep (1)

def main (num):

Global count, Mutex

Threads = []

Count = 1

# Create a lock

Mutex = Threading. Lock ()

# Create thread Objects First

For x in xrange (0, num):

Threads.append (Threading. Thread (Target=thread_main, args= (10,)))

# Start All Threads

For T in Threads:

T.start ()

# Wait for all child threads to exit in the main thread

For T in Threads:

T.join ()

# if __name__ = = ' __main__ ':

# num = 4

# # 4 Threads created

# Main (4)

#例子2

Class Test (threading. Thread): #主要以这种方式

def __init__ (Self,num):

Threading. Thread.__init__ (self)

Self._run_num=num

def run (self):

Global Count,mutex

Threadname=threading.currentthread (). GetName ()

For x in Xrange (0,int (self._run_num)):

Mutex.acquire ()

Count = Count + 1

Mutex.release ()

Print Threadname,x,count

Time.sleep (1)

If __name__== ' __main__ ':

Global count, Mutex

Threads=[]

Num=4

Count=1

#创建锁

Mutex=threading. Lock ()

#创建线程对象

For x in Xrange (0,num):

Threads.append (Test (10))

#启动线程

For T in Threads:

T.start ()

#等待子线程结束

For T in Threads:

T.join ()

Threading Method and property Description

Thread Threads Execute Objects

Lock Lock Object

Rlock Reload Lock object (recursive lock)

Condition Condition Variable object lets one thread stop waiting for other threads to meet < a > condition, such as state change, etc.

Event Generic condition variable, multiple threads can wait for an event to occur and all threads will be activated after the event occurs

Semahore waits for a lock thread to provide a similar ' waiting room ' structure

Boundedsemaphore is similar to Semahore except that it does not allow the initial value to be exceeded

A Timer is similar to Thread , except that it waits for a period of time before it starts to run.

For more multi-threaded learning, see: http://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html

Python Threading Basic Learning

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.