gpt4 book ai didi

Mac OS X 中的 Python Errno 9 错误文件描述符

转载 作者:太空狗 更新时间:2023-10-30 01:32:06 26 4
gpt4 key购买 nike

我多次运行以下代码没有任何问题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 中运行一次,之后它就会挂起。重启机器是我重新运行测试的唯一方法。

ftpserverpytest-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

最佳答案

好的,显然是 bad file descriptor in Mac OS X已知:

that's what happens if you create an IOLoop before a fork and then try to use it in the child process. If you're going to use fork, you have to do it before anything creates the singleton 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()

关于Mac OS X 中的 Python Errno 9 错误文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45438323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com