Re: [Tutor] Lock File Usage
On Mon, Jan 30, 2017 at 7:33 PM, Ben Finney wrote: > "ad^2" writes: > > > So, IF: no lock file, create a lock file, execute, delete lock file > > when finished successfully. ElSE: the script is running, exit. Then, > > cron will try again an hour later. > > > > I do not necessarily require a "lock file" just looking for a > > recommendation on a best practice with low complexity to make this > > work. > > The third-party ‘fasteners’ library provides an API for locks > https://pypi.python.org/pypi/fasteners> that you will want to > consider. > > Specifically, the interpprocess lock decorators > https://fasteners.readthedocs.io/en/latest/ > examples.html#lock-decorator> > seem to be what you want. > --- > Thanks for the help guys. This is what I ended up going with that works. Requires: lockfile, syslog and os modules pid = ('/var/lock/' + os.path.splitext(os.path.basename(__file__))[0]) try: lock = filelock.FileLock(pid) lock.acquire(timeout = 1) # my code function 1 # my code function 2 # and so on lock.release(force = True) os.unlink(pid) except (IOError, OSError) as e: syslog.syslog('%s' % e) print e ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to interact with the result of subprocess.call()
On 12/26/2016 04:48 AM, Peter Otten wrote: Jim Byrnes wrote: Is there a way to terminate subprocess and still keep LO open so pykeyboard can send it keystrokes from the script? In theory you can open Libre Office from another thread, wait a moment and then send it keystrokes from the main thread. I managed to do this with the script below. However, the procedure is very brittle; while experimenting I managed to "press" the control key without releasing it afterwards. The interval from doing it to realizing what was going on to "fixing" it (reboot) was an interesting experience... from contextlib import contextmanager import subprocess import threading import time import pykeyboard kb = pykeyboard.PyKeyboard() @contextmanager def press_key(key, kb=kb): kb.press_key(key) try: yield time.sleep(1) finally: kb.release_key(key) def open_it(filename): subprocess.call(["libreoffice", filename]) print("exiting libreoffice thread") LONG_ENOUGH = 15 # seconds # enter text into an existing odt file filename = "demo.odt" text = "hello and goodbye" opener = threading.Thread(target=open_it, args=(filename,)) opener.start() time.sleep(LONG_ENOUGH) # for libreoffice to start and open the file kb.type_string(text) with press_key(kb.control_key): kb.tap_key("s") with press_key(kb.alt_key): kb.tap_key(kb.function_keys[4]) print("exiting main thread") Peter, My apologies for taking so long to get back and thank you for the code. With the holidays and moving to a new computer/OS I didn't have much time to work on my little project. I was able to use your code and send the necessary keystrokes to manipulate LO. Thanks again. Regards, Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lock File Usage
"ad^2" writes: > Thanks for the help guys. This is what I ended up going with that works. > > Requires: lockfile, syslog and os modules You're probably aware, but for others reading this thread: The ‘lockfile’ library is explicitly deprecated by its maintainers https://pypi.python.org/pypi/lockfile/>. They have declared they're no longer maintaining it, and they recommend using a different library for locking. So long as you're aware that you are effectively accepting the burden of maintaining that library yourself, go ahead :-) -- \“I think it would be a good idea.” —Mohandas K. Gandhi (when | `\asked what he thought of Western civilization) | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor