Python concurrent programming Multi-Threading

Source: Internet
Author: User

Threads and processes, whether using methods or concepts, are almost the same, with the main distinction being that the conceptual aspects of different threads can be thought of as part of a process, which is divided into the primary and child processes, and the threads are also the main thread and child threads Threading (module to open threading) module introduction

The Multiprocess module completely imitates the interface of the threading module, which has a great similarity in the use level.

From threading import Thread  #开启超级线程变换状态import timedef sayhi (name):    time.sleep (2)    print ('%s say hello '% Name) If __name__ = = ' __main__ ':    t=thread (target=sayhi,args= (' Egon ',))    T.start ()    print (' main thread ')

The difference between opening multiple threads under one process and opening multiple sub-processes under one process

The thread is turned on much faster than the open subprocess, and the PID of the thread that is opened under the main process is the same as the PID of the main process.

From threading import threadfrom multiprocessing import processimport osdef work ():    print (' Hello ', os.getpid ()) if __ name__ = = ' __main__ ':    #part1: Open multiple threads under the main process, each thread is the same as the PID of the main process    T1=thread (target=work)    T2=thread (target= Work)    T1.start ()    t2.start ()    print (' main thread/main process pid ', Os.getpid ())    #part2: Open multiple processes, each with a different PID    p1=process (target=work)    p2=process (target=work)    P1.start ()    p2.start ()    print (' main thread/ Main process PID ', os.getpid ())


Sharing in-process data between threads in the same process

From threading Import Thread
From multiprocessing import Process
Import OS
def work ():
Global n
N=0

if __name__ = = ' __main__ ':
# n=100
# p=process (target=work)
# P.start ()
# P.join ()
# print (' main ', N) #毫无疑问子进程p已经将自己的全局的n改成了0, but instead of just its own, check that the parent process's n is still 100


N=1
T=thread (Target=work)
T.start ()
T.join ()
Print (' primary ', N) #查看结果为0 because the in-process data is shared among threads in the same process
#同一进程内的线程共享该进程的数据?

Other thread-related methods
Thread instance object's method  # 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 are:  # 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 ()).



As with the process, the main thread will wait for the child thread to finish running after the child thread is finished.

Python concurrent programming Multi-Threading

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.