我正在Raspberry Pi 4上编写一个多线程Python应用程序,偶尔需要从服务器下载约200 KB的二进制文件,在测试期间,服务器是我在本地网络上的笔记本电脑。我已经使用Curl或Python 3 CLI requests.get调用在RPi上验证了我的笔记本电脑在大约一秒钟内就提供了这些文件,但是在我的应用程序中,请求下载调用在完成之前至少会挂起2分钟。受影响的代码如下所示:
# requests current composite from server
# args: timestamp: timestamp of last update
# returns: SUCCESS_RETURN if updated, NONE_RETURN otherwise, OFFLINE_RETURN on failure to connect
def getcomposite(self, timestamp=None):
try:
self.slplogger.info("Downloading composite for timestamp %s" % (dt.utcfromtimestamp(timestamp).strftime("%Y-%m-%d-%H:%M:%S") if timestamp else "None"))
# HANGS HERE
compositeresp = requests.post(SERVER_URL + "getcomposite", data={'mac' : self.mac, 'timestamp' : timestamp})
self.slplogger.info("Downloaded new composite: %s" % str(compositeresp.text[:min(10, len(compositeresp.text))]))
if compositeresp.text != NONE_RETURN and compositeresp.content:
with self.compositelock:
self.compositedata = np.load(BytesIO(compositeresp.content), allow_pickle=False)
# compute new input norm for adding subsequent input
self.compositenorm = np.mean(self.compositedata[:]['value'], dtype=int)
self.emptycomposite = False
self.slplogger.info("Set new composite: %s" % str(self.compositedata[:min(10, len(self.compositedata))]))
return SUCCESS_RETURN
return FAILURE_RETURN
except requests.exceptions.ConnectionError:
self.slplogger.info("Composite download failed. Unable to connect to server")
return OFFLINE_RETURN
这里定义了调用此方法的非守护进程线程(复合轮询间隔为2秒):
# --------------------------------------------------------------------
# CompositePollingThread - Thread superclass that periodically
# polls strangeloop server for new additions to the composite loop
# --------------------------------------------------------------------
class CompositePollingThread(Thread):
# overloaded Thread constructor
# args: pedal: parent Pedal object that instantiated this thread
def __init__(self, pedal):
Thread.__init__(self)
self.stop = Event()
self.pedal = pedal
self.timestamp = None
self.pedal.slplogger.debug("Initialized composite polling thread")
# main thread execution loop
def run(self):
self.pedal.slplogger.debug("Started composite polling thread")
while self.pedal.running:
time.sleep(COMPOSITE_POLL_INTERVAL)
# timestamp to determine whether any new data needs to be downloaded
if not self.pedal.recording and self.pedal.getcomposite(timestamp=self.timestamp) == SUCCESS_RETURN:
self.timestamp = dt.utcnow().timestamp()
self.pedal.slplogger.debug("Downloaded new composite at %s" % dt.utcfromtimestamp(self.timestamp).strftime("%Y-%m-%d-%H:%M:%S"))
self.pedal.slplogger.debug("Ended composite polling thread")
我假设下载速度慢是由于程序中的线程问题造成的。它还负责处理占用大部分CPU的实时输入。我能做些什么来提高下载速度吗?有没有办法给线程更高的优先级,或者我应该切换到多处理模块来利用RPI4的多核
最后,问题出在服务器上。我将端点响应从一个“flask.send_文件”更改为一个简单的flask.response对象,其中包含我所需的数据(类似于Serve image stored in SQLAlchemy LargeBinary column),现在它可以在不到十秒钟的时间内下载。我不知道为什么下载只在通过程序访问时(与CLI请求相反)花费了这么长时间,但这一更改解决了它
相关问题 更多 >
编程相关推荐