近几天用Python写一个并发的计算引擎,遇到一些问题,经过一段时间的调试终于解决了。特记录如下,以供参考。
python3.5, loggiing的问题。
import logging
import concurrent.futures
import pandas as pd
import numpy as np
def test_process(): with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executors: print("Process pool executor started") future_list = [] for i in range(5): work = Work(i) pickled = pickle.dumps(work) print(pickled) future_list.append(executors.submit(call_do_work, work)) for futures in concurrent.futures.as_completed(future_list): print(str(futures.result()))
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
在Linux上,会报出以下错误:
Traceback (most recent call last):
File "./mutli_process_test.py", line 119, in
test_process()
File "./mutli_process_test.py", line 110, in test_process
pickled = pickle.dumps(work)
TypeError: cannot serialize '_io.TextIOWrapper' object
去掉下面的行
logging.basicConfig(level=logging.DEBUG)
def __getstate__(self): odict = self.__dict__.copy() del odict['logger'] return odict def __setstate__(self, state): self.__dict__.update(state) self.logger = logging.getLogger(__name__) self.logger.setLevel(logging.INFO)
以上简单的问题,如果没有遇到,不太容易找到现成的解决方案。网上的很多有关这个的讨论,也不是很直接。
如果有人遇到相同的问题,可以很快的找到思路和解决方案。