无法迭代multiprocessing.managers.DictProxy

时间:2019-06-25 06:03:43

标签: python-3.x pytest multiprocess

无法迭代multiprocessing.managers.DictProxy。通过pytest-paralell,但在python中可以正常工作。

即使在https://bugs.python.org/issue9733,我也发现了此问题,但是由于manager.py是只读的;我无法在那里进行更改。有人早些面对这个问题吗?我该如何解决?

test_Run.py

from multiprocessing import Process, Manager

def f(d, l):
    d[1] = '1'
    d['2'] = 2
    d[0.25] = None
    l.reverse()

if __name__ == '__main__':
    with Manager() as manager:
        d = manager.dict()
        l = manager.list(range(10))

        p = Process(target=f, args=(d, l))
        p.start()
        p.join()

        print(d)
        print(l)

如果您运行

(venv) [tivo@localhost src]$ python test_run.py 
{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
(venv) [tivo@localhost src]$ 

已编辑:

如果您使用pytest使用以下代码运行此代码。

from multiprocessing import Process, Manager

def test_f():
    d, l = {}, []
    d[1] = '1'
    d['2'] = 2
    d[0.25] = None
    print(d)

with Manager() as manager:
    d = manager.dict()
    l = manager.list(range(10))
    l.reverse()
    print(l)
    p = Process(target=f)
    p.start()
    p.join()

(venv) [tivo@localhost src]$ pytest -v -s test_run.py 
collecting ... [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
{0.25: None, 1: '1', '2': 2}
collected 1 item                                                                                                                                               

test_run.py::test_f {0.25: None, 1: '1', '2': 2}
PASSED

(venv) [tivo@localhost src]$

但是,如果您与pytest-paralell软件包一起运行pytest,则会引发错误

(venv) [tivo@localhost src]$ pytest -v -s --tests-per-worker auto --workers auto test_run.py 
===================================================================== test session starts ======================================================================
platform linux -- Python 3.4.4, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- /home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/src, inifile: pytest.ini
plugins: xdist-1.28.0, remotedata-0.3.1, pipeline-0.3.0, parallel-0.0.9, forked-1.0.2, flake8-1.0.4, cov-2.7.1
collecting ... [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
{0.25: None, 1: '1', '2': 2}
collected 1 item                                                                                                                                               
pytest-parallel: 2 workers (processes), 0 test per worker (thread)

Traceback (most recent call last):
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/bin/pytest", line 10, in <module>
sys.exit(main())
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/_pytest/config/__init__.py", line 79, in main
return config.hook.pytest_cmdline_main(config=config)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/hooks.py", line 289, in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/manager.py", line 68, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/manager.py", line 62, in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/callers.py", line 208, in _multicall
return outcome.get_result()
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/callers.py", line 80, in get_result
raise ex[1].with_traceback(ex[2])
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/callers.py", line 187, in _multicall
res = hook_impl.function(*args)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/_pytest/main.py", line 242, in pytest_cmdline_main
return wrap_session(config, _main)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/_pytest/main.py", line 235, in wrap_session
session=session, exitstatus=session.exitstatus
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/hooks.py", line 289, in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/manager.py", line 68, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/manager.py", line 62, in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/pluggy/callers.py", line 203, in _multicall
gen.send(outcome)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/_pytest/terminal.py", line 678, in pytest_sessionfinish
self.summary_stats()
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/_pytest/terminal.py", line 876, in summary_stats
(line, color) = build_summary_stats_line(self.stats)
  File "/home/tivo/workspace/ServicePortal/autotestscripts/CAT/scripts/ServerQE/brat/venv/lib/python3.4/site-packages/_pytest/terminal.py", line 1034, in build_summary_stats_line
for found_type in stats:
  File "<string>", line 2, in __getitem__
  File "/usr/local/lib/python3.4/multiprocessing/managers.py", line 747, in _callmethod
    raise convert_to_error(kind, result)
 KeyError: 0
(venv) [tivo@localhost src]$ 

我有以下包裹

pytest-parallel==0.0.9
pytest-pipeline==0.3.0
问:我可以采取什么解决方法来使上面的代码通过而没有错误日志?结果是问题没有让我知道通过了多少个测试用例。

为什么要服用pytest-parallel: 2 workers (processes), 1 test per worker (thread)?我在那里只提供了一个功能!

提示:

如果我添加标志--worker 1;以上错误不会出现;但通常会导致脚本失败,因此我不得不与之一起使用--tests-per-worker 1。但是,这里不存在并列现象!

0 个答案:

没有答案