Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个永无止境的python脚本之间的交互_Python_Apache Kafka_Mqtt_Publish Subscribe_Messagebroker - Fatal编程技术网

两个永无止境的python脚本之间的交互

两个永无止境的python脚本之间的交互,python,apache-kafka,mqtt,publish-subscribe,messagebroker,Python,Apache Kafka,Mqtt,Publish Subscribe,Messagebroker,我有一个mqtt代理,我已经订阅并不断接收数据 代码 因此,当我运行这个python脚本时,它是一个永远的过程,不断地更新目标变量。我想在另一个脚本中使用这个目标变量 代码 这也是一个永久的过程,每60秒运行一次作业。我想在main.py中使用broker.py中的target_变量 我无法在一个脚本中运行这两件事,如果我有单独的脚本并尝试在main.py中加载broker.py,broker.py只会执行,而不会结束 有人能帮我解决这个问题吗?请将它们作为函数,并在单个脚本中作为deamon线

我有一个mqtt代理,我已经订阅并不断接收数据

代码

因此,当我运行这个python脚本时,它是一个永远的过程,不断地更新目标变量。我想在另一个脚本中使用这个目标变量

代码

这也是一个永久的过程,每60秒运行一次作业。我想在main.py中使用broker.py中的target_变量

我无法在一个脚本中运行这两件事,如果我有单独的脚本并尝试在main.py中加载broker.py,broker.py只会执行,而不会结束


有人能帮我解决这个问题吗?

请将它们作为函数,并在单个脚本中作为deamon线程启动,这样它将永远运行,直到您停止脚本。 例如:

def myfunc1(i):
#任何你想做的工作
通过
def myfunc2():
#任何你想做的工作
通过
t1=线程(目标=myfunc1,参数=(,))
t1.daemon=True
t1.start()
t2=线程(目标=myfunc2,参数=(,))
t2.daemon=True
t2.start()
# broker.py

import paho.mqtt.client as mqtt
def on_message(client, userdata, message):
    print("message received ")
    # do some calculations on the data recieved.
    target_variable  = #stored after the calculations.

client.on_message=on_message #attach function to callback

print("connecting to broker")
client.connect(broker_address, port=port,) #connect to broker
client.subscribe("topic")
client.loop_forever() #stop the loop

# main.py

import schedule
from broker.py import target_variable


def job():
    # use target_variable and perform some taks
    print(target_variable)

schedule.every(60).seconds.do(job)


while True:
    schedule.run_pending()