Python simple process lock code instance, python instance

Source: Internet
Author: User

Python simple process lock code instance, python instance

Let's talk about threads first

In multithreading, thread synchronization is often used to ensure the correctness of shared resources.
Convert some sensitive operations into atomic operations to ensure that only one thread is executing the atomic operation in multiple threads at the same time.
I usually use mutex locks, also known as exclusive locks. There are also read/write locks, semaphores, and conditional variables.
In addition, we use signals to send signals to a process during inter-process communication. In this process, we set the signal processing function, and then execute some operations when the process receives signals.
In fact, the thread can also accept signals. Using this mechanism, we can also implement thread synchronization. For more information, see http://www.bkjia.com/article/64977.htm

Process

In the process, we can implement inter-process synchronization through some inter-process communication methods.
Recently, many processes in a collection system process pool print logs to the same log file.
Another way is to share the memory. Different processes put log messages in a thread-safe queue through the shared memory, and then create a process to print logs, in this way, we can avoid chaos,
Ensure the log correctness, but the amount of code is also large.
Another way is to set a mutex lock in the shared memory for all processes to share.
If there is a simple mutex lock like a thread, as long as the lock is used, it will be able to achieve mutual exclusion between processes. Previously, I was somewhat impressed with the file lock, so I used it to implement a mutex lock between processes.

#coding=utf-8  """ Process mutex lock. Actually it is implemented by file lock. """  import fcntl  class ProcessLock(object):      __lockfd = None      @staticmethod     def lock():         ProcessLock.__lockfd = open(__file__, 'a+')         fcntl.flock(ProcessLock.__lockfd, fcntl.LOCK_EX)      @staticmethod     def unlock():         fcntl.flock(ProcessLock.__lockfd, fcntl.LOCK_UN) 


Lock ProcessLock. lock ()
Release ProcessLock. unlock ()
It is very easy to use. If you are interested, you can try it.

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.