对CouchDB的并行调用

2024-05-20 23:57:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Python3对CloudDB集群进行压力测试,但是我不能让多线程正常工作。我用过Google,有很多方法可以做到这一点,其中大多数对我和/或这个用例来说都很复杂。在

我要做的是同时在3个节点上将一个文档保存到CouchDB。 我该如何使用最简单的多线程方法来实现这一点呢?在

import couchdb
import random
import time
import _thread

servers = {
    "pizerogrijs": "http://admin:admin@pizerogrijs.local:5984/",
    "pizerogeel": "http://admin:admin@pizerogeel.local:5984/",
    "pizeroroze": "http://admin:admin@pizeroroze.local:5984/"
}

databasename = 'testhijs'

class bank(object):
    def __init__(self):
        self.dbs = {}
        for s in servers:
                self.dbs[s] = couchdb.Server(servers[s])[databasename]

    def showdbs(self):
        print(self.dbs)

    def randomwrite(self, data):
        randomdb = self.dbs[random.choice(list(self.dbs))]
        return(randomdb.save(data))

    def directwrite(self, server, data):
        start = time.time()
        directdb = self.dbs[server]
        end = time.time()
        print(directdb.save(data))
        print(end - start)

def streskip(server):
    db.directwrite(server, {"Test": "Thijs"})

db = bank()

_thread.start_new_thread(streskip('pizerogrijs'), ())
_thread.start_new_thread(streskip('pizerogeel'), ())
_thread.start_new_thread(streskip('pizeroroze'), ())

while 1:
   pass

答复是:

^{pr2}$

Tags: importselfhttpdataserveradmintimelocal
1条回答
网友
1楼 · 发布于 2024-05-20 23:57:31

我需要在元组中添加参数,而不是直接添加。像这样。在

_thread.start_new_thread(streskip, ('pizerogrijs',))
_thread.start_new_thread(streskip, ('pizerogeel',))
_thread.start_new_thread(streskip, ('pizeroroze',))

其余代码都很好,是我能找到的最简单的多线程方式。在

相关问题 更多 >