이것은 스크립트가 파이썬에서 실행되는 경우 해결책입니다 (또는 다음 명령 전에 실행하기 위해 파이썬 명령을 만들 수 있습니다)-지난 주 에이 정확한 문제가 발생했지만 좋은 해결책을 찾았지만 매우 결정했습니다. 간단하고 깨끗한 파이썬 패키지를 PyPI에 업로드했습니다. 잠금 __file__할 사용자 정의 리소스 이름을 제공하는 것에 대해 생각할 필요가 없도록 확실히 잠글 수 있습니다.
다음을 사용하여 설치 : pip install quicklock
그것을 사용하는 것은 매우 간단합니다 :
[nate@Nates-MacBook-Pro-3 ~/live] python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from quicklock import singleton
>>> # Let's create a lock so that only one instance of a script will run
...
>>> singleton('hello world')
>>>
>>> # Let's try to do that again, this should fail
...
>>> singleton('hello world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/nate/live/gallery/env/lib/python2.7/site-packages/quicklock/quicklock.py", line 47, in singleton
raise RuntimeError('Resource <{}> is currently locked by <Process {}: "{}">'.format(resource, other_process.pid, other_process.name()))
RuntimeError: Resource <hello world> is currently locked by <Process 24801: "python">
>>>
>>> # But if we quit this process, we release the lock automatically
...
>>> ^D
[nate@Nates-MacBook-Pro-3 ~/live] python
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from quicklock import singleton
>>> singleton('hello world')
>>>
>>> # No exception was thrown, we own 'hello world'!
살펴보십시오 : https://pypi.python.org/pypi/quicklock