[英]How can I send multiple “user input” to python script from another python script?
[英]How can I send a value from a python script to another script?
例如第一个脚本:
from secondScript import Second
---
""
""
""
while True:
lastResult = <a list> --> I need to send this result to other script
---
我的另一个剧本
class Second:
def __init__(self):
""
""
""
self.dum = Thread(target=self.func1)
self.dum.deamon = True
self.dum.start()
self.tis = Thread(target=self.func2, args= <a list>)
self.tis.deamon = True
self.tis.start()
def func1(self):
while True:
""
""
""
def func2(self, lastResult):
while True:
print(lastResult)
因此,我想将我在第一个脚本中找到的值发送到脚本 2 中的无限线程 function。我无法将第一个脚本导入第二个,因为我还从脚本 2 中获取另一个值到脚本 1。
编辑:
我们可以这样想:我的程序有一部分已经在运行了。 我们可以说我正在从相机获取实时图像。 整个代码在运行的同时,也在不断地、不间断地产生一个数值。 所有这些操作都在第一个文件中完成。 当第一个文件继续工作时,它需要不断地将这个数字发送到第二个文件。 在第二个代码中,同时运行了 2 个不同的无限循环函数。 在第1个function中,从arduino中不断读取数据,不间断。 第二个 function 应该打印来自第一个代码的数字。 所以实际上我无法在代码 1 中更改任何内容。我正在生成数字值。 我需要以某种方式将它发送到代码 2。 我不确定如何编辑您编写的代码。 任何睡眠等。我不能使用任何中断方法,因为在代码 1 中,相机应该不间断地工作。
在第一个脚本中:
print(lastResult, end='\n', file=sys.stdout, flush=True)
在其他脚本和其他线程中:
second = Second()
...
for lastResult in sys.stdin:
lastResult = lastResult[:-1]
second.func2(lastResult)
...
好的,这回答了你的问题,但我改变了一点结构。 我会这样做:
编辑:如果你有像你提到的 CameraFeed 这样的连续数据流,你可以使用具有这样模式的队列(在这种情况下你真的不需要第二个 class,你可以在不同的类中实现 CameraFeed 和 CameraDataConsumer) .
如果dum线程没有向tis线程发送数据,可以使用tis的send_data方法通过main function向其发送数据,并从CameraFeed中移除队列。
from threading import Event, Thread
from queue import Queue, Full
import time
class CameraFeed(Thread):
def __init__(self, queue):
super().__init__()
# This event is used to stop the thread
# Initially it is unset (False)
self._stopped_event = Event()
self._queue = queue
self._data = []
def run(self):
# sample_data is for demo purposes remove it and
# fetch data however you do it
sample_data = iter(range(10**10))
# Loop as long as the stopped event is not set
while not self._stopped_event.is_set():
# Get data from camera this is mock data
data = next(sample_data)
# Put data in the list for data to be sent
self._data.append(data)
# Get the next available item from the list
data = self._data.pop(0)
try:
# This tries to puts in the queue
# if queue is at max capacity raises a Full Exception
self._queue.put_nowait(data)
print('CameraFeed, sent data:', data)
except Full:
# If exception occures, put the data back to list
self._data.insert(0, data)
def stop(self):
# Sets the stopped event, so the thread exits the run loop
self._stopped_event.set()
class CameraDataConsumer(Thread):
def __init__(self, queue):
super().__init__()
self._stopped_event = Event()
self._queue = queue
def run(self):
while not self._stopped_event.is_set():
# Waits for data from queue
data = self._queue.get(block=True)
# If data is None then do nothing
if data is None:
continue
print('CameraConsumer, got data:', data)
def send_data(self, data):
"""Method to send data to this thread from main probably"""
self._queue.put(data, block=True)
def stop(self):
# Set the stopped event flag
self._stopped_event.set()
# Try to put data to queue, to wake up the thread
try:
self._queue.put_nowait(Data(None, EventType.OPERATION))
except Full:
# If queue is full, don't do anything it is probably
# safe to assume that setting the stop flag is sufficient
print('Queue is full')
# Create a Queue with capacity 1000
queue = Queue(maxsize=1000)
dum = CameraFeed(queue)
dum.start()
tis = CameraDataConsumer(queue)
tis.start()
# time.sleep is for demo purposes
time.sleep(1)
tis.stop()
dum.stop()
tis.join()
dum.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.