web.py 的问题 + mod_wsgi文件上传
我正在开发一个用于上传文件的 web.py 应用程序,但我的部署遇到了真正的问题。基本上我想给用户一个“上传的百分比”,但这在 mod_wsgi 上部署时似乎会严重混乱。主要上传的工作原理如下:
out = open(path, 'wb', 1000)
while (True):
packet = fileU.file.read(1000)
if not packet:
break
else:
out.write(packet)
sessions[code].progress += 1
out.close()
“Session”是一个全局字典,其中包含跟踪会话的对象。为了获取当前进度,我每秒通过客户端发出的 GET 请求获取给定会话的当前进度。
目前的问题是这仅适用于小上传。看起来超过 100kb 左右的任何内容都不会增加进度变量。如果放置在循环之外(或在调用 read() 之前)或者文件相当小,则该值肯定会递增。
wsgi 是否有可能为更大的文件打开新线程,从而使我的全局计数器仅在上传线程本地?会不会是别的什么。
I'm working on a web.py app for uploading files and im having real problems with my deployment. Basically I want to give the user a 'percentage uploaded' but this seems to be messing up severely when deployed on mod_wsgi. The main upload works like this:
out = open(path, 'wb', 1000)
while (True):
packet = fileU.file.read(1000)
if not packet:
break
else:
out.write(packet)
sessions[code].progress += 1
out.close()
'Session' is a global dictionary that contains objects that keep track of sessions. To get the current progress I get the current progress for a given session via a GET request from the client every second.
The problem at the moment is that this only works for small uploads. It seems that anything over around 100kb will just not increment the progress variable. The value is definitely incremented if placed outside of the loop (or before read() is ever called) or if the file is fairly small.
Is it possible that wsgi is opening new threads for bigger files and therefore making my global counter only local to the upload thread? Could it be something else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能仅使用全局字典来实现此目的。事实上,网络服务器很可能使用单独的线程来服务以下请求,并且不能保证会话字典是相同的。尝试使用属于 web.py 一部分的会话对象。它使用数据库或文件来存储可以跨不同线程或进程访问的数据。
web.py 会话示例
You can't just use a global dictionary for this. It is in fact very likely that the webserver is using a separate thread to serve the following requests, and there is no guarantee that the session dictionary is the same. Try using the session object which is part of web.py. It uses either a db or a file to store the data which can be accessed across different threads or processes.
web.py session example