无法从多处理中运行一个过程
我在测试我的浏览器应用程序时一直尝试运行一个进程。该过程是我进行 pytest 测试的固定装置之一,它应该在我开始测试时运行。但是,当我运行它时,它显示错误。我很困惑,因为这应该是一个简单的过程。这是我的代码:
@pytest.fixture(scope='class')
def chrome_driver(request):
""" Selenium webdriver with options to support running in GitHub actions
Note:
For CI: `headless` and `disable-gpu` not commented out
For running on your computer: `headless` and `disable-gpu` to be commented out
"""
options = ChromeOptions()
#options.add_argument("--headless") # use for GitHub Actions CI
#options.add_argument('--disable-gpu') # use for GitHub Actions CI
options.add_argument("--window-size=1920,1080")
chrome_driver = Chrome(options=options)
request.cls.driver = chrome_driver
yield
chrome_driver.close()
@pytest.fixture(scope='class')
def run_app(app):
"""
Fixture to run the Flask app for Selenium browser tests
"""
multiprocessing.set_start_method("spawn") # Needed in Python 3.8 and later
#process = multiprocessing.Process(target=app.run, args=())
#process.start()
process = multiprocessing.Process(target=app.run, args=())
process.start()
process.join()
yield process
process.terminate()
上面是我测试的两个装置,它们将在我开始测试时同时运行。下面是错误:
test setup failed
app = <Flask 'flask_app'>
@pytest.fixture(scope='class')
def run_app(app):
"""
Fixture to run the Flask app for Selenium browser tests
"""
multiprocessing.set_start_method("spawn") # Needed in Python 3.8 and later
#process = multiprocessing.Process(target=app.run, args=())
#process.start()
process = multiprocessing.Process(target=app.run, args=())
> process.start()
..\conftest.py:138:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py:121: in start
self._popen = self._Popen(self)
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py:224: in _Popen
return _default_context.get_context().Process._Popen(process_obj)
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py:327: in _Popen
return Popen(process_obj)
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\popen_spawn_win32.py:93: in __init__
reduction.dump(process_obj, to_child)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
obj = <Process name='Process-1' parent=26100 initial>
file = <_io.BufferedWriter name=11>, protocol = None
def dump(obj, file, protocol=None):
'''Replacement for pickle.dump() using ForkingPickler.'''
> ForkingPickler(file, protocol).dump(obj)
E AttributeError: Can't pickle local object 'Flask.__init__.<locals>.<lambda>'
I have been trying to run a process when testing my browser app. The process is one of my fixture for pytest testing, and it should be running when i start my test. However, it showed error when i run it. I am quite confused because it should be a simple process. this is my code:
@pytest.fixture(scope='class')
def chrome_driver(request):
""" Selenium webdriver with options to support running in GitHub actions
Note:
For CI: `headless` and `disable-gpu` not commented out
For running on your computer: `headless` and `disable-gpu` to be commented out
"""
options = ChromeOptions()
#options.add_argument("--headless") # use for GitHub Actions CI
#options.add_argument('--disable-gpu') # use for GitHub Actions CI
options.add_argument("--window-size=1920,1080")
chrome_driver = Chrome(options=options)
request.cls.driver = chrome_driver
yield
chrome_driver.close()
@pytest.fixture(scope='class')
def run_app(app):
"""
Fixture to run the Flask app for Selenium browser tests
"""
multiprocessing.set_start_method("spawn") # Needed in Python 3.8 and later
#process = multiprocessing.Process(target=app.run, args=())
#process.start()
process = multiprocessing.Process(target=app.run, args=())
process.start()
process.join()
yield process
process.terminate()
above are the two fixtures for my testing, and they will be run the same time when i start my test. Below is the errors:
test setup failed
app = <Flask 'flask_app'>
@pytest.fixture(scope='class')
def run_app(app):
"""
Fixture to run the Flask app for Selenium browser tests
"""
multiprocessing.set_start_method("spawn") # Needed in Python 3.8 and later
#process = multiprocessing.Process(target=app.run, args=())
#process.start()
process = multiprocessing.Process(target=app.run, args=())
> process.start()
..\conftest.py:138:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py:121: in start
self._popen = self._Popen(self)
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py:224: in _Popen
return _default_context.get_context().Process._Popen(process_obj)
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py:327: in _Popen
return Popen(process_obj)
..\..\..\..\AppData\Local\Programs\Python\Python310\lib\multiprocessing\popen_spawn_win32.py:93: in __init__
reduction.dump(process_obj, to_child)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
obj = <Process name='Process-1' parent=26100 initial>
file = <_io.BufferedWriter name=11>, protocol = None
def dump(obj, file, protocol=None):
'''Replacement for pickle.dump() using ForkingPickler.'''
> ForkingPickler(file, protocol).dump(obj)
E AttributeError: Can't pickle local object 'Flask.__init__.<locals>.<lambda>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我设置了多处理。set_start_method(“ fork”)并在IDE内部运行单个测试,那么我就可以得到类似的测试进行运行。一旦我在测试套件中运行它,我会出现“泡菜”错误。
I was able to get my similar test to run if I instead set multiprocessing.set_start_method("fork") and running the single test inside the IDE. As soon as I run it in a test suite I get the 'pickle' error though.
正如亨利指出的那样,分叉将避免泡菜错误。在pytest会话中将会话范围内的固定装置设置为一次执行。
As Henry pointed out, forking will avoid the pickle error. Set in a session scoped fixture to executed once in pytest session.