Two ways to process and create threads (threading. Thread)

Source: Internet
Author: User

Process such as QQ to a whole form of exposure to the operating system management, which contains a variety of resource calls, memory management, network interface calls, etc., process is a collection of various resource management

Thread: is the smallest dispatch unit of operating system, is a combination of a series of instructions

Process to operate the CPU, you must create a thread

1 so threads in the same process are sharing the same block of memory space

Data for 22 sub-processes is not shared and cannot be accessed by each other, and thread data is shared

3. The thread of the same process can communicate, data sharing, information transfer, two processes want to communicate, must pass an intermediary agent

4. Creating a new thread is simple, creating a new process requires a clone of its parent process

5. One thread can control and manipulate other threads, but the process can only manipulate child processes

6. Modifications to the main thread may affect other threads, but the process will only manipulate the child processes

The first way to create a process: a function call

1 Importthreading, time2 3 defrun (n, run_time):4     Print('Task Run%s'%N)5 time.sleep (run_time)6     Print('The run%s'%N)7 8 9 Ten  One  Am = Threading. Thread (Target=run, args= (5, 1))#Child Thread -  -M1 =threading. Thread (Target=run, args= (2, 3)) the  - M.start () - M1.start () -  +M1.join ()#executes the child thread M1 -M.join ()#executes the child thread M2 +  A  at Print('Alex')

The second way to create a process: Inherit threading. Thread

Importthreading, timeclassMyThread (Threading. Thread):def __init__(self, N, run_time): Threading. Thread. __init__(self) SELF.N=N self.time=Run_timedefRun (self):Print('Task run%s'%SELF.N) time.sleep (self.time)Print('The run%s'%SELF.N) M= MyThread (1, 5) M1= MyThread (3, 2) M.start () M1.start () M.join (timeout=2)#Executes in two seconds and goes down .M1.join (1)#Execute one second and go down .Print('Task Done')

Two ways to process and create threads (threading. Thread)

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.