Day9 Python Learning Essays

Source: Internet
Author: User

First, threading

Question:

    • application, process, thread relationships?

    • Why use multiple CPUs?

    • Why use multithreading?

    • Why use multiple processes?

    • What is the difference between multithreading in Java and C # and Python multi-threading?

    • Python GIL?

    • Choice of threads and processes: compute-intensive and IO-intensive programs. (IO operation does not consume CPU)

1. Python Thread

Threading is used to provide thread-related operations, which are the smallest unit of work in an application.

#!/usr/bin/env python#-*-coding:utf-8-*-import threadingimport time def Show (ARG): Time.sleep (1) print ' thread ' +s TR (ARG) for I in range: T = Threading. Thread (target=show, args= (i,)) T.start () print ' main thread stop '

The code above creates 10 "foreground" threads, then the controller is handed over to the CPU,CPU according to the specified algorithm for scheduling, shard execution instructions.


More ways:

    • Start thread is ready to wait for CPU scheduling

    • SetName setting a name for a thread

    • GetName Get thread Name

    • Setdaemon set to background thread or foreground thread (default)
      if it is a background thread, during the main thread execution, the background thread is also in progress, and after the main thread finishes executing, the background thread stops regardless of success or not.
      If it is the foreground thread, during the main thread execution, the foreground thread is also in progress, and after the main thread finishes executing, wait for the foreground thread to finish, the program stops

    • The join executes each thread one by one, and continues execution after execution ...

    • Execute this method after the run thread is dispatched by the CPU

2. Thread Lock

The CPU then executes other threads because the threads are randomly dispatched, and each thread may execute only n execution. Therefore, the following problems may occur:

#!/usr/bin/env python# -*- coding:utf-8 -*-import  threadingimport timegl_num = 0def show (ARG):     global gl_ Num    time.sleep (1)     gl_num +=1    print  gl_numfor i in range ():     t = threading. Thread (target=show, args= (i,))     t.start () print  ' Main thread stop ' The thread lock 
is not used
#!/usr/bin/env python#coding:utf-8 Import threadingimport Time Gl_num = 0 lock = Threading.      Rlock () def Func (): Lock.acquire () global gl_num gl_num +=1 time.sleep (1) Print Gl_num lock.release () For I in range: T = Threading. Thread (Target=func) T.start ()


This article is from the "Li-Enlightened" blog, please make sure to keep this source http://jzzjw.blog.51cto.com/9921462/1726991

Day9 Python Learning Essays

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.