Java and python processes can exclusively lock the same files on Linux

I have a Java application and a python launcher Java applications use this code to lock files to avoid multiple starts:

java.nio.channels.FileLock lock = lockWrapper.getChannel().tryLock();
if (lock == null) {
     logger.info("Anotheris already running");
}
lock.release();
staticLock = lockWrapper.getChannel().lock();

The python launcher attempts to lock the same file with fcntl, and it can Two Java processes cannot do this, and neither Python process can exclusively lock on the same file But Java and python can be in any direction I'm using openjdk 6 and python2 7 xubuntu, I use portal locker for Python

lockfile =open(lockfilename,'w+')
    portalocker.lock(lockfile,portalocker.LOCK_EX| portalocker.LOCK_NB)

It can also work normally on win7

Solution

I got the answer from my colleague, which is very simple Java does not use POSIX locks, but Python can So they can't interoperate

In fact, they can, but only if one person can force two runtimes to use the same locking mechanism But this forces you to hard code it, making the code fragile and very platform dependent

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>