Python 对CouchDB的并行调用
我想用Python3对CloudDB集群进行压力测试,但我无法让多线程正常工作。我使用过谷歌,有很多方法可以做到这一点,其中大多数方法对我和/或这个用例来说都很复杂 我试图做的是将一个文档同时保存到3个节点上的CouchDB。 我将如何使用最简单的多线程方式来实现这一点Python 对CouchDB的并行调用,python,multithreading,python-3.x,Python,Multithreading,Python 3.x,我想用Python3对CloudDB集群进行压力测试,但我无法让多线程正常工作。我使用过谷歌,有很多方法可以做到这一点,其中大多数方法对我和/或这个用例来说都很复杂 我试图做的是将一个文档同时保存到3个节点上的CouchDB。 我将如何使用最简单的多线程方式来实现这一点 import couchdb import random import time import _thread servers = { "pizerogrijs": "http://admin:admin@pizero
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
答复是:
/usr/bin/python3.6 /home/thijs/Repos/DataBos/test.py
('f4e66074fdae56c6de6b1d744033eb63', '1-a8238505469902134486a6744284a43a')
9.5367431640625e-07
Traceback (most recent call last):
File "/home/thijs/Repos/DataBos/test.py", line 39, in <module>
_thread.start_new_thread(streskip('pizerogrijs'), ())
TypeError: first arg must be callable
Process finished with exit code 1
/usr/bin/python3.6/home/thijs/Repos/DataBos/test.py
('f4e66074fdae56c6de6b1d744033eb63','1-A823850546902134486A6744284A43A')
9.5367431640625e-07
回溯(最近一次呼叫最后一次):
文件“/home/thijs/Repos/DataBos/test.py”,第39行,在
_thread.start_new_线程(streskip('pizerogrijs'),())
TypeError:第一个参数必须是可调用的
进程已完成,退出代码为1
我需要在元组中添加参数,而不是直接添加。像这样
_thread.start_new_thread(streskip, ('pizerogrijs',))
_thread.start_new_thread(streskip, ('pizerogeel',))
_thread.start_new_thread(streskip, ('pizeroroze',))
剩下的代码很好,是我能找到的最简单的多线程方式。问题是什么?“我想使用最简单的多线程方式。”