The difference between threads and processes and Python code instances _python

Source: Internet
Author: User

In the World of program apes, threads and processes are a very important concept, and many people are often confused about what threads and processes are, and what the difference is, this article tries to explain threads and processes. First look at the concept:

Processes (English: process), which is the entity of a program that is already running on your computer. The process is the basic unit of operation that used to be a time-sharing system. In process-oriented systems (such as earlier versions of Unix,linux 2.4 and earlier), processes are the basic execution entities of a program, and in thread-oriented systems such as most contemporary operating systems, Linux 2.6, and newer versions, the process itself is not a basic unit of operation, but a container for threads. The program itself is simply a description of the instructions, the data, and its organization, and the process is the actual running instance of the program (those instructions and data). – Wikipedia

Threads (English: Thread) is the smallest unit in which the operating system can perform operational scheduling. It is included in the process and is the actual unit of operation in the process. A thread refers to a single sequential control flow in a process in which multiple threads can be concurrent and each thread performs different tasks in parallel. – Wikipedia

The concept is too scary to take a look at the process, which is a little bit easier to understand than a thread. Process is the entity that the program is running, meaning that the program is stored on the hard disk, and when the program runs, there are several processes that are visible, and in the WINDOWS8 task Manager we see the following processes:

So what is a thread? If you've written a program, especially a small number of logical control statements like Windows batch, it's obvious that when you run a program, the whole process runs from head to tail. For example, code that has such a batch, prompts the user for a name, and then prints the greeting according to the name:

@echo off
set/p name= Please enter your name: 
cls
echo Hello,%name
pause

This small program is executed from top to bottom, exit after execution completes. Whether it's a batch, like Php,node.js, and so on, it's done top-down. In fact, this is a thread that understands that a thread is a task flow that is contained within a process. Here's an example:

May 1 This day, McDonald's business is more popular, a lot of people, the front desk has 6 windows, there are 4 windows in the work, with more people to eat, McDonald's has to open the remaining two windows. Here, each window is a process, dealing with the task of selling junk food, so that the system needs to deal with more requests, the open window is to increase the process to deal with requirements. Because it is a holiday, it is found that even 6 windows full open, queuing customers are still many, then, here is no other way? Efficiency is forced out, the manager found that customers buy things, and so on, when the customer's hamburger (or other junk food) is ready, it is by a single person (xiaoming) to the customer, because the person needs to be prepared for food to 6 different Windows customers, so inefficient. When the manager spoke, the food was ready, and the seller of the window directly gave the food to the customers who were waiting, so it was more time-saving. Here, the job of the receptionist is to have one of the original work of selling goods, become two, is two process.

In summary, a process of at least one thread, in the actual work, not all programs support multithreading, there are some programs for many processes also support not good enough, like php,node.js and so are single process, single thread.

Here's a python script that runs two threads in a process:

Import time 
import thread 
def ordering (interval): 
  cnt = 0 
  while cnt<100: 
    print ' OK, your order is successful, the reservation number is :%d order time is:%s Please wait patiently beside \ n '% (CNT, time.ctime ()) 
    time.sleep (interval) 
    cnt+=1 
  thread.exit_thread 
() def Notice (interval): 
  cnt = 0 
  while cnt<100: 
    print ' whose number is%d, your meal is ready, come and Fetch \ n '% (CNT) 
    Time.sleep ( Interval) 
    cnt+=1 
  thread.exit_thread ()  
  
def work (): #Use thread.start_new_thread () to create 2 new Threads 
  Thread.start_new_thread (Ordering, (1,)) 
  Thread.start_new_thread (Notice, (5,)) 
  
if __name__= = ' __main__ ': 
  work ()

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.