我有一个应用程序(通常)一次只尝试一个 http 发布一个或两个文件,但这可能会导致要上传的文件积压(基于文件大小/网络速度)。我想我可能会尝试一个线程队列系统,以便用户可以继续填充队列,而在后台,线程队列会咀嚼要发送的文件。
我试图扼杀这个答案来工作,但无济于事。
这是我的代码:
import threading, urllib2, Queue, os
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
path = "path/to/files/"
urls_to_load = [os.path.join(path,x) for x in os.listdir(path)]
def read_url(file, queue):
register_openers()
datagen, headers = multipart_encode({"myfile": open(file, "rb")})
request = urllib2.Request("http://localhost/appname/upload", datagen, headers)
queue.put(urllib2.urlopen(request).read())
def fetch_parallel():
result = Queue.Queue()
threads = [threading.Thread(target=read_url, args = (url,result)) for url in urls_to_load]
for t in threads:
t.start()
for t in threads:
t.join()
return result
result = Queue.Queue()
for url in urls_to_load:
read_url(url,result)
print result
但是,我收到权限错误:
Traceback (most recent call last):
File "C:/Users/tmiller/Desktop/multiBro.py", line 78, in <module>
datagen, headers = multipart_encode({"myfile": open(i, "rb")})
IOError: [Errno 13] Permission denied: 'the file'
我认为发生了一些访问冲突,但我该如何防止这种情况发生?
Web 服务器本质上是在运行this。谢谢!