该类ConnectionPool
适用于偶尔需要向机架空间发送某些内容的多线程应用程序。
这样,您可以重用您的连接,但如果您有 100 个线程,则不必保持 100 个连接打开。
您只是在寻找一个多线程/多处理上传器。multiprocessing
这是使用该库的示例:
import cloudfiles
import multiprocessing
USERNAME = '---'
API_KEY = '---'
def get_container():
conn = cloudfiles.get_connection(USERNAME, API_KEY)
testcontainer = conn.create_container('test')
return testcontainer
def uploader(filenames):
'''Worker process to upload the given files'''
container = get_container()
# Keep going till you reach STOP
for filename in iter(filenames.get, 'STOP'):
# Create the object and upload
obj = container.create_object(filename)
obj.load_from_filename(filename)
def main():
NUMBER_OF_PROCESSES = 16
# Add your filenames to this queue
filenames = multiprocessing.Queue()
# Start worker processes
for i in range(NUMBER_OF_PROCESSES):
multiprocessing.Process(target=uploader, args=(filenames,)).start()
# You can keep adding tasks until you add STOP
filenames.put('some filename')
# Stop all child processes
for i in range(NUMBER_OF_PROCESSES):
filenames.put('STOP')
if __name__ == '__main__':
multiprocessing.freeze_support()
main()