Python Forum
A loop for autocompile LaTeX file takes high CPU
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A loop for autocompile LaTeX file takes high CPU
#1
I tried to make a program to autocompile a LaTeX document on Linux Mint whenever the file changes. My effort is as follows:
import subprocess
import sys
import time
from datetime import datetime


# This function compiles the document.
def execute(command):
    err = subprocess.STDOUT
    a = subprocess.check_call(command, stdout=subprocess.DEVNULL, stderr=err)
    return a


modified = datetime.now()
print(modified)
compiled_once = False
while True:
    time.sleep(1)
    try:
        execute(['pdflatex', '-interaction=nonstopmode', '/home/user/a.tex'])
        print('File okay.')
    except subprocess.CalledProcessError:
        print('error')
I think it takes very high CPU usage, and thought that sleep(1) will reduce usage as the document won't be compiled too often. How can I reduce the usage?
Reply
#2
The correct way is to use a module that watches the filesystem to detect when the file is modified, such as « inotify » (or rather « pyinotify ») or « watchdog ». I posted another one here that uses the module « doit » that can also watch the filesystem (probably through pyinotify in Linux).

My program runs a python script automatically when the script is modified. It is straightforward to modify it to run automatically «pdflatex» when a tex file is modified.

Edit: inotify-simple looks like a particularly interesting module to consider as it is a minimal wrapper of linux' inotify. The documentation shows how to write a stoppable thread that watches a directory for example.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nbconvert Jupyter to LaTeX ross1993hall 3 2,782 Jul-13-2020, 09:11 AM
Last Post: ross1993hall
  Latex spacing question StillAnotherDave 0 1,494 Mar-28-2020, 05:51 PM
Last Post: StillAnotherDave
  GPIO time in HIGH LOW boris_za 1 2,282 Dec-07-2019, 01:48 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020