Open maldororxul opened 3 weeks ago
As a quick fix you can probably import trio
before monkey patching. This will cache trio with un-patched system things.
There has to be a way to fix this that isn't just a game of whack-a-mole but I don't have any ideas.
If I import trio before monkey patching it will fall with "AttributeError: module 'select' has no attribute 'epoll'".
I ended up with this to avoid both attribute errors:
def monkey_patch_trio():
try:
import select
if not hasattr(select, 'epoll'):
class FakeEpoll:
def __init__(self, *args, **kwargs):
raise NotImplementedError("epoll is not supported on this platform")
fake_select_module = ModuleType("select")
fake_select_module.epoll = FakeEpoll
sys.modules["select"] = fake_select_module
# Patch GreenSocket before trio._socket is imported
from eventlet.greenio.base import GreenSocket as EventletGreenSocket
if not hasattr(EventletGreenSocket, 'sendmsg'):
setattr(EventletGreenSocket, 'sendmsg', lambda *args, **kwargs: None)
if not hasattr(EventletGreenSocket, 'recvmsg'):
setattr(EventletGreenSocket, 'recvmsg', lambda *args, **kwargs: None)
if not hasattr(EventletGreenSocket, 'recvmsg_into'):
setattr(EventletGreenSocket, 'recvmsg_into', lambda *args, **kwargs: None)
# Now import trio._socket
import trio._socket as trio_socket
import socket as _stdlib_socket
from functools import wraps as _wraps
class PatchedSocketType(trio_socket.SocketType):
@_wraps(_stdlib_socket.socket.sendmsg, assigned=(), updated=())
def sendmsg(self, *args, **kwargs):
raise NotImplementedError("sendmsg is not supported on this platform")
@_wraps(_stdlib_socket.socket.recvmsg, assigned=(), updated=())
def recvmsg(self, *args, **kwargs):
raise NotImplementedError("recvmsg is not supported on this platform")
@_wraps(_stdlib_socket.socket.recvmsg_into, assigned=(), updated=())
def recvmsg_into(self, *args, **kwargs):
raise NotImplementedError("recvmsg_into is not supported on this platform")
trio_socket.SocketType = PatchedSocketType
except ImportError:
pass
Sorry, I mean import trio
before running eventlet.monkey_patch()
.
I'm trying to work with openai package that uses trio on Flask app hosted on Heroku.
First it failed with 'epoll issue': https://github.com/python-trio/trio/issues/2848 I tired to fix it with monkey_patch, but got a new issue:
AttributeError: type object 'GreenSocket' has no attribute 'sendmsg'
My entry point with patch:
Dependencies:
Error logs:
Here starts the problem: