Python implements the script lock function (only one script can be executed at the same time) and python script

Source: Internet
Author: User

Python implements the script lock function (only one script can be executed at the same time) and python script

1. file lock

Check whether a specific file exists before the script starts. If the file does not exist, start and create a new file. After the script ends, delete the specified file.

Determine whether the script is being executed by checking the file.

The method implementation is also relatively simple. Here, the python script is used as an example.

# Coding = UTF-8 ## file lock Script test # import OS # operating system import time lockfilepath = "/opt/lock.txt"; # determine whether the file exists if OS. path. exists (lockfilepath): # The file exists, indicating that the script is executing print ("the script is executing") else: # the file does not exist. Create the file and execute the task. Lockfile = open (lockfilepath, "w +"); print ("file lock Task Simulation sleep 10 s"); time. sleep (10); print ("task simulation ends sleep over"); # delete the file OS. remove (lockfilepath );

File lock test results

2. Port occupation

Use the socket module to add a port to the system during script execution and release the port after the script is executed.

When the script is executed, create the port. If the script is still being executed, the port creation will fail, prompting that the script is still being executed.

The implementation method is also relatively simple: Python script

# Coding = UTF-8 import time import socket suo = 1 try: global hyf_suo = socket. socket () addr = ('', 98889) hyf_suo.bind (addr); suo = 1; region T: suo = 2; print ('already has an instance ') if (suo = 1): print ("lock sleep"); time. sleep (10); print ("sleep over"); else: print ("locked ");

Port lock Test Result

Conclusion

Both of the above methods can implement the script lock function. individuals prefer to use the second method. Port lock method.

The above section describes how to implement the script lock function in Python (only one script can be executed at the same time). I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.