Skip to content

Python3 - subprocess.Popen

Python3
  • Für ein UI zum Tool Restic, an dem ich jetzt schon eine ganze Zeit "code", brauche ich einen Prozess der asynchron läuft.

    Ok, ich versuche das mal als Laie zu erklären. Ich starte oft Prozesse mit

    process = subprocess.run(['restic','version'], check=True, stdout=subprocess.PIPE, universal_newlines=True)
    

    Das gibt mir die Version von Restic zurück. Dieser Prozess wird gestartet und beendet!

    Nun wollte ich aber eine Sicherung mounten. Und dieser Prozess in Restic, wird gestartet und läuft, so lange bis man ihn mit CTRL + C auf der Konsole beendet. Jo, damit war dann meine Denksportaufgabe für gestern definiert. Ich muss eingestehen, es hat was länger gedauert 😀

    Nach einiger Zeit hatte ich rausbekommen, das ich es so machen muss.

    p1 = subprocess.Popen(cmd,stdin=subprocess.PIPE)
    

    Wir starten den Prozess und übergeben ihm den Restic Befehl (cmd) und setzen als stdin die subprocess.PIPE. Damit wird der Prozess unabhängig gestartet, mein Python3 Script wartet nicht mehr auf das Ende. So nach dem Motto, gestartet und vergessen 🙂

    Da tauchte das nächste Problem auf. Restic erwartet eine Passwort Eingabe. Ja, ich möchte das übergeben und ja ich weiß das ist aus Sicherheitsaspekten evt. nicht der beste Ansatz. Egal, da kümmere ich mich später drum!

    Viele Anleitungen im Netz gefunden, aber irgendwie war ich zu blöde die umzusetzen. Nur ein Ansatz machte dann bei mir das, was ich erwartete und auch teilweise verstand.

    # Wir schreiben das erwartete Passwort als Eingabe
    p1.stdin.write('12345678'.encode())
    p1.stdin.flush()
    p1.stdin.close()
    msg_box(self, "Repository erfolgreich gemountet!")
    

    Erste Zeile

    Ohne das .encode() funktioniert das nicht. Dem Prozess Popen muss das als Bytes Stream übergeben werden!

    This means that the string is converted to a stream of bytes, which is how it is stored on any computer. As bytes!

    Wir schreiben also diesen Bytes Stream in das Objekt p1
    Quelle: https://www.askpython.com/python/string/python-encode-and-decode-functions

    pwd='12345678'
    

    Und so sieht das aus, als Bytes Stream

    b'12345678'
    

    Zeile 2

    Die kann ich mir leider nicht erklären.

    Zeile 3

    Damit wird die Verbindung zum Prozess p1 geschlossen. Ohne schließen funktioniert es nicht.

    Wenn jemand weiß, wie man das Vernünftig erklären kann, wäre ich sehr dankbar.

    Fazit

    Der Prozess ist erfolgreich gestartet, läuft unabhängig vom Python Script. Das Passwort wird auch erfolgreich übergeben Das was ich brauchte 🙂 Wieder ein Stück weiter und was gelernt!!

  • Python - Match-Case Statement

    Python3
    1
    0 Stimmen
    1 Beiträge
    82 Aufrufe
    Niemand hat geantwortet
  • Restic - Kompression

    Restic
    2
    0 Stimmen
    2 Beiträge
    257 Aufrufe
    FrankMF

    Gestern Abend noch ein paar Tests gemacht, aber nicht wirklich Erfolg gehabt. Ok, dann heute noch mal von vorne und mit System. Als erstes muss man mal Daten finden, die man auch gut komprimieren kann, ich will ja auch ein deutliches Ergebnis sehen.

    Meine Wahl fiel auf einen openwrt Ordner, mit dem ich mal ein Image selber gebaut hatte. Schön viele kleine Dateien, sollte sich gut komprimieren lassen.

    Original

    547e3596-69df-48ac-9409-5173367afb85-grafik.png

    Test mit 7z

    Rechtsklick, mit den Bordmittel und dann 7z gewählt.

    364b497f-cf59-408c-ba2b-cad70cc09529-grafik.png

    Test mit Restic V1

    Ich habe auf einer mechanischen 1TB Platte zwei Ordner angelegt, einmal Restic_V1, einmal Restic_V2.

    frank@frank-MS-7C37:~/Downloads$ restic version restic 0.13.1 compiled with go1.18 on linux/amd64 Init restic init -r /media/1TB/Restic_V1/ Backup frank@frank-MS-7C37:~/Downloads$ restic -r /media/1TB/Restic_V1/ backup /home/frank/openwrt/ enter password for repository: repository 731db857 opened successfully, password is correct created new cache in /home/frank/.cache/restic no parent snapshot found, will read all files Files: 407839 new, 0 changed, 0 unmodified Dirs: 41286 new, 0 changed, 0 unmodified Added to the repo: 7.851 GiB processed 407839 files, 11.061 GiB in 4:49 snapshot 24cd8ef4 saved Ergebnis

    799fa3ee-5bdf-48ba-a05e-8f7f24f1c41b-grafik.png

    Test mit Restic V2 frank@frank-MS-7C37:~/Downloads$ ./restic_v0.13.0-126-g26c33332_linux_amd64 version restic 0.13.1-dev (compiled manually) compiled with go1.18 on linux/amd64 Init ./restic_v0.13.0-126-g26c33332_linux_amd64 init -r /media/1TB/Restic_V2/ --repository-version 2 Backup frank@frank-MS-7C37:~/Downloads$ ./restic_v0.13.0-126-g26c33332_linux_amd64 -r /media/1TB/Restic_V2/ backup /home/frank/openwrt/ enter password for repository: repository 33c5e24c opened (repo version 2) successfully, password is correct created new cache in /home/frank/.cache/restic no parent snapshot found, will read all files Files: 407839 new, 0 changed, 0 unmodified Dirs: 41286 new, 0 changed, 0 unmodified Added to the repo: 7.835 GiB processed 407839 files, 11.061 GiB in 2:47 snapshot 474d0376 saved Ergebnis

    caafd946-1285-4e1d-8873-a3ff4141a777-grafik.png

    Fazit

    Fassen wir es mal ein wenig zusammen. Das Original hat 12,1GB

    ITool Dateigröße Zeit 7z 2,2GB ca. 11:59 Restic V1 8,5GB 4:49 Restic V2 2,9GB 2:47

    Man kann auch noch etwas an der Kompression einstellen, ich habe es für diesen Test auf der Standardeinstellung(?) gelassen.

    You can set the desired compression level by passing it to --compression (e.g. restic backup --compression max), supported are auto, max and off.

    Das Ergebnis sieht sehr vielversprechend aus. Es könnte den Platzverbrauch stark begrenzen, sehr wichtig für mich wenn man seine Daten in einer Cloud speichert. (Stichwort: Kosten) Und was hier auch noch schön ins Auge fällt, es ist schneller 🙂 Das möchte ich hier nicht versuchen zu erklären, da ich nicht genau woran es liegt. Vermutung, ich muss wesentlich weniger Daten "schreiben".

    Ich freue mich extrem, diese Version produktiv einzusetzen. Mal überlegen, ob ich die Version hier auf dem Haupt-PC mal testweise nutze, ich denke das wäre spannend.

    @Restic-Team: Vielen Dank für dieses tolle Feature!
  • Restic UI - QThread

    Restic UI
    1
    0 Stimmen
    1 Beiträge
    95 Aufrufe
    Niemand hat geantwortet
  • 0 Stimmen
    2 Beiträge
    179 Aufrufe
    FrankMF

    Die REST Unterstützung ist komplett 🤓

    Link Preview Image Not Found

    favicon

    (gitlab.com)

    Ich denke, ein wenig Dokumentation wäre noch gut ☺

  • 0 Stimmen
    1 Beiträge
    168 Aufrufe
    Niemand hat geantwortet
  • checkmk - Rest-Server überwachen

    Verschoben checkmk
    1
    0 Stimmen
    1 Beiträge
    340 Aufrufe
    Niemand hat geantwortet
  • Python3 - virtuelle Entwicklungsumgebung

    Angeheftet Python3
    3
    0 Stimmen
    3 Beiträge
    301 Aufrufe
    FrankMF

    Nach Systemwechsel erneut Probleme. Hier noch mal aufgelistet.

    Aufpassen, das kein venv Ordner vorhanden ist! Neu anlegen!

    python3 -m venv venv

    Dann meckert mein Linux Mint Cinnamon

    The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command. apt install python3.8-venv You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment. Failing command: ['/home/frank/Restic_UI_Produktiv/restic-ui-public/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

    Ok, das ist einfach 😉

    apt install python3.8-venv python3 -m venv venv

    Danach aktivieren

    source venv/bin/activate Installation von PyQt5 (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install PyQt5 Collecting PyQt5 Downloading PyQt5-5.15.4-cp36.cp37.cp38.cp39-abi3-manylinux2014_x86_64.whl (8.3 MB) |████████████████████████████████| 8.3 MB 1.7 MB/s Collecting PyQt5-Qt5>=5.15 Downloading PyQt5_Qt5-5.15.2-py3-none-manylinux2014_x86_64.whl (59.9 MB) |████████████████████████████████| 59.9 MB 18.3 MB/s Collecting PyQt5-sip<13,>=12.8 Downloading PyQt5_sip-12.9.0-cp38-cp38-manylinux1_x86_64.whl (332 kB) |████████████████████████████████| 332 kB 46.1 MB/s Installing collected packages: PyQt5-Qt5, PyQt5-sip, PyQt5 Successfully installed PyQt5-5.15.4 PyQt5-Qt5-5.15.2 PyQt5-sip-12.9.0

    Danach meckert er über ein fehlendes Modul requests

    (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ python3 restic_ui.py Traceback (most recent call last): File "restic_ui.py", line 41, in <module> from functions import ( File "/home/frank/Restic_UI_Produktiv/restic-ui-public/functions.py", line 19, in <module> import requests as req ModuleNotFoundError: No module named 'requests' Installation requests (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install requests Collecting requests Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB) |████████████████████████████████| 61 kB 802 kB/s Collecting chardet<5,>=3.0.2 Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB) |████████████████████████████████| 178 kB 2.7 MB/s Collecting certifi>=2017.4.17 Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB) |████████████████████████████████| 145 kB 14.0 MB/s Collecting idna<3,>=2.5 Downloading idna-2.10-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 9.4 MB/s Collecting urllib3<1.27,>=1.21.1 Downloading urllib3-1.26.6-py2.py3-none-any.whl (138 kB) |████████████████████████████████| 138 kB 11.5 MB/s Installing collected packages: chardet, certifi, idna, urllib3, requests Successfully installed certifi-2021.5.30 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.6

    Und schwupps, geht mein Tool wieder 🙂

    4968ce42-7c66-4c8a-a2ad-424b9a529d87-grafik.png

  • 0 Stimmen
    1 Beiträge
    1k Aufrufe
    Niemand hat geantwortet