4

I am trying to write a small clipboard logger (for linux) that listens for an event (the change of the clipboard content) and writes the clipboard content to a file (on-change).

What I have come up with is a simple while loop with the clipboard module pyperclip:

import pyperclip

recent_value = ""
while True:
    tmp_value = pyperclip.paste()
    if tmp_value != recent_value:
        recent_value = tmp_value
        with open(".clipboard_history.txt", "a") as f:
            f.write(recent_value + "\n")

So my first question is, can I actually run a while True loop to 'listen' or will this consume too much memory or be generally inefficient or bad practice?

And the second question is, how can I run this in the background like a shell job control (ampersand)?

Should I go for a daemon like suggested here or some kind of event loop or threading magic?

I basically want something that sits in the background and listens for an event (clipboard content changes), reacts on it (writes to a file) and waits again.

============================

edit: Thanks for the input! + new question: Would I still need the sleep method if I used threading?

upgrd
  • 720
  • 7
  • 16
  • Threading would be wasted on this code, because this code does this and only this. However if your program wanted to do something else, whilst this code is running, then and only then would you want to use threading. Another exception would be if you got a GUI(would go under program do something else tho) –  Jun 26 '17 at 20:34

4 Answers4

3

Running your current loop will drain CPU, import from time, and use time.sleep(1) or something else that would put the program to sleep for a little while (ideally 0.1-2~ seconds if you think they would be swapping copy/paste fast)

You don't need to thread if this is all your program is going to be doing.

1

So my first question is, can I actually run a while True loop to 'listen' or will this consume too much memory or be generally inefficient or bad practice?

It is inefficient. You probably want to add at the end of your loop

time.sleep(0.1)

Better practice would be to run your script every time clipboard is being written to. This discussion is also relevant to you: Trigger an event when clipboard content changes

And the second question is, how can I run this in the background like a shell job control (ampersand)?

Refer to here.

Joonatan Samuel
  • 641
  • 4
  • 17
1

First of all, you need to fix your indentation as the following:

while True:
    tmp_value = pyperclip.paste()
    if tmp_value != recent_value:
        recent_value = tmp_value
        with open(".clipboard_history.txt", "a") as f:
            f.write(recent_value + "\n")

Now if your script has more code, and you want this loop to keep running in the background, you can use threading and define a function for the loop:

from threading import Thread
t = Thread(target=your-func, args=[])
t.start()

def your-func(self):
    # your loop goes here
Mohd
  • 5,523
  • 7
  • 19
  • 30
0

To run a python file with no console the extension should be .pyw, for example when running logger.pyw it will be ran without opening a python shell.

Hope this answered your question.

isak
  • 37
  • 7