简体   繁体   English

如何在两个正在运行的 python 程序之间链接变量?

[英]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.我正在使用 Pynput 创建一个程序,简而言之,该程序将在按住某个键的同时执行某些操作。

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.在对 Pynput 进行了一些研究之后,我发现没有办法,而且似乎没有计划的方法可以在按住键的同时做某事,所以我正在设计我的方法。

My plan is to have two Python scripts running simultaneously that have a constantly updating variable linked between them.我的计划是让两个 Python 脚本同时运行,它们之间有一个不断更新的变量链接。 This is because while loops stop Pynput listeners when used in the one program.这是因为 while 循环在一个程序中使用时会停止 Pynput 侦听器。 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).唯一的问题是我不知道如何在两个正在运行的脚本之间主动链接变量,互联网上没有任何内容让我知道如何这样做(我尝试导入其他脚本和东西,但不仅是这很难,因为我使用的是 Mac,但它没有主动传递变量)。

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.我想在它们之间链接的变量是 doThing,但同样我不知道我将如何以这种方式实际设置变量。 I was considering using JSON, but I don't know if that's the best way to do it.我正在考虑使用 JSON,但我不知道这是否是最好的方法。

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.这允许 Python 运行两个(或更多)单独的线程,每个线程同时做不同的事情。 (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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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