我有以下代码运行后多次没有任何问题 在Linux中相互对话:
def test_ftp(ftpserver):
with FTP() as f:
f.connect("localhost", port=ftpserver.server_port)
f.login("M1", "bachmann")
f.cwd("/")
f.mkd("FOO")
f.quit()
相同的测试只能在MacOS X中运行一次,之后它就会挂起。重新启动计算机是我重新运行测试的唯一方法。
ftpserver
是pytest-localftpserver中定义的测试夹具,我在这里发布此夹具的代码,原因是我怀疑它是导致错误的原因:
class MPFTPServer(multiprocessing.Process):
def __init__(self, username, password, ftp_home, ftp_port, **kwargs):
self._server = SimpleFTPServer(username, password, ftp_home, ftp_port)
self.server_home = self._server.ftp_home
self.anon_root = self._server.anon_root
self.server_port = self._server.ftp_port
super().__init__(**kwargs)
def run(self):
self._server.serve_forever()
def join(self):
self._server.stop()
def stop(self):
self._server.stop()
@pytest.fixture(scope="session", autouse=True)
def ftpserver(request):
"""The returned ``ftpsever`` provides a threaded instance of
``pyftpdlib.servers.FTPServer`` running on localhost.
...
"""
from pytest_localftpserver.plugin import MPFTPServer
ftp_user = os.getenv("FTP_USER", "fakeusername")
ftp_password = os.getenv("FTP_PASS", "qweqwe")
ftp_home = os.getenv("FTP_HOME", "")
ftp_port = int(os.getenv("FTP_PORT", 0))
server = MPFTPServer(ftp_user, ftp_password, ftp_home, ftp_port)
# This is a must in order to clear used sockets
server.daemon = True
server.start()
yield server
server.join()
你能说出为什么这段代码"重复工作"在Linux但不在MacOSX中?
进一步挖掘,我发现ftp服务器甚至不会启动,因此挂起。代码崩溃并显示以下消息:
Process MPFTPServer-1:
Traceback (most recent call last):
File "/opt/pkg/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap
self.run()
File "/Users/w/.virtualenvs/controller_config/lib/python3.5/site-packages/pytest_localftpserver/plugin.py", line 81, in run
self._server.serve_forever()
File "/Users/w/.virtualenvs/controller_config/lib/python3.5/site-packages/pyftpdlib/servers.py", line 207, in serve_forever
self.ioloop.loop(timeout, blocking)
File "/Users/w/.virtualenvs/controller_config/lib/python3.5/site-packages/pyftpdlib/ioloop.py", line 348, in loop
poll(soonest_timeout)
File "/Users/w/.virtualenvs/controller_config/lib/python3.5/site-packages/pyftpdlib/ioloop.py", line 709, in poll
timeout)
OSError: [Errno 9] Bad file descriptor
答案 0 :(得分:1)
好的,显然bad file descriptor in Mac OS X已知:
如果你在fork之前创建一个IOLoop会发生什么 然后尝试在子进程中使用它。如果您要使用fork, 在创建单例IOLoop之前你必须这样做。
所以解决方案只是在run
方法中启动服务器实例,而不是__init__
:
class MPFTPServer(multiprocessing.Process):
def __init__(self, username, password, ftp_home, ftp_port, **kwargs):
self.username = username
self.password = password
self.server_home = ftp_home
self.server_port = ftp_port
super().__init__(**kwargs)
def run(self):
self._server = SimpleFTPServer(self.username, self.password,
self.server_home, self.server_port)
self._server.serve_forever()