Python Multithreading (1)

Source: Internet
Author: User

Processes (Process)

A process is an instance of a program's memory (a process is an execution activity of a program, it is dynamic)

The implementation of multi-channel programming is to open the instance of multiple programs in memory at the same time, giving an external illusion of concurrency.

Defects in the process:

1, the process can only do one thing at the same time, if you want to do two things at the same time, the process is powerless.

2, when the process is executing a task, if it is blocked (for example, waiting for I/O), the entire program will be suspended

Threads (thread)

A thread is the smallest unit that the operating system can perform operations on, is contained in a process, is the actual operating unit in the process, and a thread refers to a single sequential control of the process.

A process can be concurrent with multiple threads, and each thread performs a different task.

The difference between a process and a thread:

1, shared memory addresses between threads (with process creation)

2, threads can communicate directly between them, and processes cannot

3, creating a thread is very quick and easy, creating a process that requires copying the complete program data

4, threads can control other threads in the same process, and processes can only control child processes

Python implements multi-threaded modules:

Threading

Simple multi-threaded concurrency program

1, call directly

#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportThreading,timedeffoo (num):Print("running on number {}.". Format (num)) Time.sleep (5)if __name__=='__main__': T1= Threading. Thread (target=foo,args= (1,)) T2= Threading. Thread (target=foo,args= (1,)) T1.start () T2.start ( )Print(T1.getname ())Print(T2.getname ())

2, inheriting type

#!/usr/bin/env Python3#-*-coding:utf-8-*-ImportThreading,timeclassMythread (Threading. Thread):def __init__(self,num): Threading. Thread.__init__(self) self.num=NumdefRun (self):Print("Running at number%s."%self.num) Time.sleep (5) T1= Mythread (1) T2= Mythread (2) T1.start () T2.start ()

Regardless of the form in which multiple threads are implemented, the threads are concurrent at run time.

Python Multithreading (1)

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.