Python tips (1)

Source: Internet
Author: User

1. The global statement is used to declare that X is global.

 

2. multi-thread programming in Python

Threading. active_count () # returns the number of threading thread objects in the alive state. current_thread () # returns the current thread object threading. enumerate () # returns the set of thread objects in the alive state.

 

3. <> and! = Equivalent, only two different styles

 

4. multi-threaded Python programming, which must be used when the program needs to concurrently process multiple tasks.

It can inherit the Thread class threading. thread, reload the member function run, write the code processed by the program in the function run, and then call the START () method to run the thread. The join () method can be used to wait for the thread to end.

Multi-threaded resource synchronization. You can use thread. rlock () to create a resource lock, and then use acquire () to lock the resource, release () to release the resource, waiting for the event to use thread. event (), wait () is used to wait for the event, set () is used to stimulate the event, and clear () is used to eliminate the triggered event.

 

5. wscript. Shell provides a set of functions to read system information, such as reading and writing the registry, searching for the path of the specified file, reading DOS environment variables, and reading the location in the link. The following is an example.

shell = win32com.client.Dispatch("WScript.Shell")shell.run('iexplore')

The run function has three parameters. The first parameter is the path of the program to be executed, the second parameter is in the window format, 0 is run in the background, and 1 indicates that the program runs normally, 2 indicates that the program is activated and displayed as minimal. The second parameter has about 10 acceptable values. The third parameter indicates whether the script is waiting or continues to be executed. If it is set to true, the script will wait for the called program to exit and then continue execution. Run is used as a function. There is a return value before it. If it is 0, it is normal; otherwise, it is an error code.

The complete code for calling this function is as follows.

import win32comimport win32com.clientshell = win32com.client.Dispatch("WScript.Shell")shell.Run("notepad", 3, True)variable = shell.Run("calc")print variable

The final output value is 0.

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.