![](/img/trans.png)
[英]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.