You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to incorporate calls to fsppec-based packages (adlfs, s3fs) into a (sync) flask view.
Flask is running using flask-socketio, which is in turn using gevent.
Whenever I try and call a filesystem method (e.g. ls), it raise the following error NotImplementedError: Calling sync() from within a running loop
I can bypass the issue by telling flask-socketio to use threading and not gevent (and remove the patching), however this has limitations (i.e. it uses werkzeug server, which is dev only) which means it can't be used in production.
[2024-10-02 16:57:35,406] ERROR in app: Exception on /ls [GET]
Traceback (most recent call last):
File "<path>/site-packages/flask/app.py", line 2073, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<path>/site-packages/flask/app.py", line 1519, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<path>/site-packages/flask/app.py", line 1517, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "<path>/site-packages/flask/app.py", line 1503, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "server.py", line 26, in listing
res = new_fs.ls(azure_path)
^^^^^^^^^^^^^^^^^^^^^
File "<path>/site-packages/fsspec/asyn.py", line 118, in wrapper
return sync(self.loop, func, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<patch>/site-packages/fsspec/asyn.py", line 80, in sync
raise NotImplementedError("Calling sync() from within a running loop")
NotImplementedError: Calling sync() from within a running loop
When you do patch_all, a number of the low-level things within asyncio change for the whole session. In addition, it appears that although your code is itself sync, the flask runtime is still running an event loop to handle requests.
I'm not sure how these things interact. The specific error should only happen if fsspec is sharing the same loop as the calling code, so you might want to check what threads are active at the time (threading.enumerate()). In the end, you might have no choice but to make your handlers async and run fsspec calls in async mode.
I'll see what I can find out via threading.enumerate().
Unfortunately I don't think I'll be able to make all the views async, since that will have far-reaching repercussions on the codebase, which I was hoping to avoid at the moment.
I am trying to incorporate calls to fsppec-based packages (adlfs, s3fs) into a (sync) flask view.
Flask is running using flask-socketio, which is in turn using gevent.
Whenever I try and call a filesystem method (e.g.
ls
), it raise the following errorNotImplementedError: Calling sync() from within a running loop
I can bypass the issue by telling flask-socketio to use
threading
and notgevent
(and remove the patching), however this has limitations (i.e. it uses werkzeug server, which is dev only) which means it can't be used in production.minimal working example:
server.py
results in
requirements.txt
Is there a known workaround for this? I have tried various alternatives, to no avail.
e.g.
_ls
asyncio.run(fs.ls)
The text was updated successfully, but these errors were encountered: