#-*-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