I have a java app and a python launcher for it. The java app locks a file to avoid multiple startups using this code:
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 tries to lock on the same file with fcntl and it can. Two java processes can’t do this and neither two python processes can lock exclusively on the same file. But a java and a python can, in any directions.
I’m on a xubuntu with openjdk 6 and python2.7 I use portalocker for python.
lockfile =open(lockfilename, 'w+')
portalocker.lock(lockfile, portalocker.LOCK_EX| portalocker.LOCK_NB)
Also work fine on win7.
I’ve got the answer from a co-worker and it’s quite simple. Java does not use the POSIX locks, but python does. So they can’t inter operate…
Actually they could, but only if one can force both runtimes to use the same locking mechanisms. But that forces you to hardcode it making the code fragile and very platform dependent.