Interpreting Python applications

Source: Internet
Author: User

The Python application provides many powerful graphic processing capabilities and supports a wide range of graphic file formats. It can be used to connect to the mysql database in zope, I hope this article will bring you the information you want.

This method is basically the opposite of join. When we execute a main thread while the program is running, if the main thread creates another sub-thread, the main thread and sub-thread will be divided into two ways. When the main thread completes and wants to exit, it checks whether the Sub-thread is complete. If the sub-thread is not completed, the main thread will wait for the sub-thread to complete and then exit.

But sometimes we need to exit with the main thread as long as the main thread is completed and the thread is not completed, the setDaemon method can be used. If you want to create a Thread object, it is very simple, as long as your class inherits the Python application, and then first calls the _ init _ method of threading. Thread in _ init:

This is just an empty thread. I don't want him to pull the empty car. He has to do some real work for me. It's easy to rewrite the run () method of the class and put all the things you want to do during thread execution into it. The above Code allows this thread to output information to the screen every second after execution. After 10 times, the getName () is threading. A Method of the Thread class to obtain the name of the Thread object. Another method, setName (), is to set the name of the thread object.

Isn't the Python application started? Why is it not called the "running" status? There is actually a reason. Because our computers generally do not have real parallel processing capabilities. We call multithreading to divide the time into segments, and then let a thread execute each time period, and then enter the "sleeping" state, and then wake up another thread in the "sleeping" state.

So the loop runnable-> sleeping-> runnable... is just because the computer runs fast and the interval between time segments is very small, we don't feel it. We thought it was done at the same time. Therefore, after a thread starts, it is only in a running state. When will it be scheduled by the system. When will a thread "dead?

Generally, when the run method of the thread object ends or an exception is thrown during execution, the thread ends. The system automatically clears the "dead" status thread.

 
 
  1. Import threading
  2.  
  3. Import time
  4.  
  5. Class myThread (threading. Thread ):
  6.  
  7. Def _ init _ (self, threadname ):
  8.  
  9. Threading. Thread. _ init _ (self,Name=Threadname)
  10.  
  11. Def run (self ):
  12.  
  13. Time. sleep (5)
  14.  
  15. Print self. getName ()
  16.  
  17. Def fun1 ():
  18.  
  19. T1.start ()
  20.  
  21. Print 'fun1 done'
  22.  
  23. Def fun2 ():
  24.  
  25. T2.start ()
  26.  
  27. Print 'fun2 done'
  28.  
  29. T1=MyThread('T1 ')
  30.  
  31. T2=MyThread('T2 ')
  32.  
  33. T2.setDaemon (True)
  34.  
  35. Fun1 ()
  36.  
  37. Fun2 ()
  38.  
  39. In the preceding example, we assume that:
  40. Fun1 done
  41.  
  42. Fun2 done
  43.  
  44. T1

But in the interaction mode, the main thread is terminated only when the Python application exits, so the result T2.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.