Two ways to turn on threads,

Source: Internet
Author: User

‘‘‘
Catalogue
1, two ways to turn on threading * * * * *
2, the difference between threads and processes * * *
3, other methods and properties of the thread object
4, Daemon thread
5, Mutual exclusion lock
6, deadlock phenomenon with pass-through lock
7, Signal volume
‘‘‘
#1 two ways to turn on threads
Import time
From threading Import Thread


def dask (name):
Print ('%s is running '%name)
Time.sleep (1)
Print ('%s is do '%name)


T=thread (target=dask,args= (' Egon ',))
T.start ()
Print (' master ')

# The Second Kind
Class Mythread (Thread):
def __init__ (self,name):
Super (Mythread, self). __init__ ()
Self.name=name

def run (self):
Print ("%s is running"%self.name)

Print ('%s is do '%self.name)


T=mythread (' Alex ')
T.start ()
Print (' master ')

#--------------------Process vs thread vs difference--------------------
#1, the thread is open fast, the thread executes the code no longer survives
# All non-daemons will die after the main thread executes, because the main process is going back to the operating system to receive resources
# If the child thread is not finished, the child thread cannot find the data

From threading Import Thread
From multiprocessing import Process
Import time

def task (name):
Print ('%s is running '%name)
Time.sleep (2)
Print ('%s is do '%name)

if __name__ = = ' __main__ ':
T=thread (target=task,args= (' Child Threads ',))
T.start ()
Print (' master ')


#2. Multiple threads under the same process share data within the process, and child threads can modify the data within that process to be called by other child threads
#子进程修改全局变量修改的只是自己的, the child process cannot modify the resources of the parent process because the space is physically isolated
From threading Import Thread
Import time
x=100
Def dask ():
Global X
x=88
Print (x)
Time.sleep (1)


#开启多个子进程多个子进程的PID are different, open multiple sub-threads, multiple child threads and the main thread of the PID are consistent

T=thread (Target=dask)
T.start ()
T.join () #会等子线程运行完毕才往下走
Print (' main ', X)

#查看pid
From threading Import Thread
Import Os,time

Def task ():
Print (Os.getpid ())

T=thread (Target=task)
T.start ()
Print (' Master ', Os.getpid ())

# ====================== Other methods and properties of thread objects ==================#
#主进程等子进程是因为主进程要给子进程收尸
#进程必须等待其内部所有线程都隐形完毕才结束
From threading Import Current_thread,active_count,enumerate
Import time

def task (name):
Print ('%s is running '%name)
Time.sleep (2)
Print ('%s is do '%name)

T=thread (target=task,args= (' Child Threads ',))
T.start ()
Print (' master ')


# ------------------------------------------
Def task ():
Print ('%s is running '%current_thread (). Name)
Time.sleep (2)
Print ('%s is done '%current_thread (). Name)#查看当前进程的名字

Print (' walk up here ')
T=thread (target=task,name= ' xxx ')
T.start ()
T.join ()
Print (T.is_alive ()) #判断进/thread is alive
Print (T.getname ()) #拿到线程对象的名字xxx
Print (Enumerate ())

Current_thread (). SetName (' main thread ')
Print (' Master ', Current_thread (). Name) #拿到当前线程的名字

# Methods for thread instance objects
# isAlive (): Returns whether the thread is active.
# getName (): Returns the thread name.
# SetName (): Sets the thread name.

# Some of the methods provided by the threading module:
# Threading.currentthread (): Returns the current thread variable.
# threading.enumerate (): Returns a list that contains the running thread. Running refers to threads that do not include pre-and post-termination threads until after the thread has started and ends.
# Threading.activecount (): Returns the number of running threads with the same result as Len (Threading.enumerate ()).

Two ways to turn on threads,

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.