简体   繁体   中英

How can I link a variable between two running python programs?

I'm using Pynput to create a program which, in short, will do something whilst a key is held down.

After doing some research into Pynput I have found that there is no way and there doesn't seem to be a planned method to do something whilst a key is held down, so I'm designing my way around it.

My plan is to have two Python scripts running simultaneously that have a constantly updating variable linked between them. This is because while loops stop Pynput listeners when used in the one program. One of these scripts will be listening to the keyboard and update the variable accordingly, and the other will be actually executing the result.

The only issue is that I have no idea how to actively link a variable between two running scripts, and nothing on the internet has given me an inkling as to how to do so (I have tried importing other scripts and things, but not only was that difficult because I am using a Mac, but it didn't actively pass a variable).

Currently, my code looks a little bit like this:

(Listener Script)

from pynput import keyboard

doThing = 0

def on_press(key):
    doThing = 1

def on_release(key):
    doThing = 0

def startListener():
    listener = keyboard.Listener(
        on_press=on_press,
        on_release=on_release)
    listener.join()

(Script that Does Something)

while True:
    if doThing == 1:
        print('Thing')

The variable I want to link between them would be doThing, but again I don't know how I would actually set the variable up that way. I was considering using JSON, but I don't know if that's the best way to do it.

Thanks!

You already consider use a temp file ? here is the example:

from pynput import keyboard

doThing = 0

def generate_variable(var): 
    with open("temp", "a") as temp:
        temp.write(str(var)) 

def on_press(key):
    generate_variable(1)

def on_release(key):
    doThing = 0

def startListener():
    listener = keyboard.Listener(
        on_press=on_press,
        on_release=on_release)
    listener.join()

On the second script:

def truncate_file(): 
    with open("temp","w"): 
        pass 

while True:
    doThing = len(open("temp", "r").read()) > 0
    if doThing:
        print('Thing')
        truncate_file()

Here is an example using threading. This allows Python to run two (or more) separate threads, each doing different things at the same time. (Technically they're not actually at the same time, but both happening alternately, but that's not important in this case).

In one thread, you listen for key presses. And in the other thread, you check for the key state and react appropriately.

import threading
from pynput import keyboard

class KeyCheckThread(threading.Thread):
    def __init__(self):
        super(KeyCheckThread, self).__init__()
        self.doThing = 0

    def on_press(self, key):
        self.doThing = 1

    def on_release(self, key):
        self.doThing = 0

    def run(self):
        with keyboard.Listener(on_press=self.on_press, on_release=self.on_release) as listener:
            listener.join()


listenerThread = KeyCheckThread()
listenerThread.start()

while(True):
    if listenerThread.doThing == 1:
        print("doThing")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM