我已经编写了一个python代码来从Multiple redis实例中提取指标。首先,我编写了一个非常简单的代码,其中将包含redis实例主机详细信息列表。使用条件,我连接到每个redis实例并提取指标。
现在,我尝试使用python多处理来增强代码。我对Python很陌生。下面是我尝试启用多处理的代码。但是显示错误。
是否有人可以帮助您指出问题所在,也请指导如何解决此过程。任何示例代码都会对我有很大帮助。
#!/usr/bin/python
import time
import redis
import multiprocessing
redis_host = ['localhost','192.168.56.10']
def conn_redis(host):
r = redis.Redis(host=host,port=6379)
return r
def main(r):
try:
print r.info()
except redis.ConnectionError,e:
print e
if __name__ == '__main__':
p = multiprocessing.Pool(multiprocessing.cpu_count())
redis_conn = p.map(conn_redis, redis_host)
while(True):
main(redis_conn)
time.sleep(5)
错误消息:
Traceback (most recent call last):
File "redis-metrics-thread.py", line 21, in <module>
redis_conn = p.map(conn_redis, redis_host)
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 250, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 554, in get
raise self._value
multiprocessing.pool.MaybeEncodingError: Error sending result: '[Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>]'. Reason: 'PicklingError("Can't pickle <type 'function'>: attribute lookup __builtin__.function failed",)'