Using the client in a Web Application
See original GitHub issueMorning, We have a standard web application (in our case Django, but the example holds for most cases including Flask, CherryPy, etc.,)…
I’d have expected this to work inside a function in my web app
with vt.Client(api.api_key) as client:
file_info = client.get_object(
'/files/44d88612fea8a8f36de82e1278abb02f')
return file_info.sha256
But it doesn’t unfortunately. This is due to the _make_sync
call which tries to get the current event loop and run the awaitable
to completion. I believe this will cause a fair bit of consternation for most web applications.
Any suggestions on how the library is intended to be used inside web applications? Here is the traceback
Traceback (most recent call last):
File "/..../actions/vt_get_file.py", line 47, in process
file_info = client.get_object(
File "/.../lib/python3.8/site-packages/vt/client.py", line 416, in get_object
return _make_sync(self.get_object_async(path, *path_args, params=params))
File "/.../lib/python3.8/site-packages/vt/client.py", line 47, in _make_sync
return asyncio.get_event_loop().run_until_complete(future)
File "/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Client Web Application - HTML5 Builder - Embarcadero DocWiki
A client web application is a web application based on client-side web technologies. You can create a new client web application from Home...
Read more >What is the difference between a web application and a client ...
A "Web Application" is one in which a browser is commonly used as the client. A Web application IS A Client/Server Application.
Read more >What is a Web Client and How It Works with Web Server ...
A Web client is an application installed on the user's device that they can use to surf the internet. Web clients request computer...
Read more >Securing the access between a client and backend services of ...
The user is going to use an application to get some tasks done and access the web client of the application. The client...
Read more >Difference Between Client Server Application and Web ...
An application that runs on the client side and accesses the remote server for information is called a client/server application whereas an application...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
vt-py’s version 0.5.4 has been released, which should fix this issue. Could you please update it and let me know if everything goes as it should?
Your understanding of how an async library should work is not incorrect but a different approach: typically, libraries are meant to be executed synchronously but have the capability of being executed in an asynchronous way. Our approach is the opposite as you could see by reading the code: vt-py is meant to be executed asynchronously, but we also provide a way to use it in a synchronous way.
The issue with Django (and Celery, and CherryPy) is that WSGI applications doesn’t define an event loop by themselves on the threads they spawn when handling web requests, as this is a term introduced by the internal Async IO module, which is a relatively new approach of parallel programming with Python, and those WSGI environments were originally developed under the “old” mindset (old = before Python 3.4).
This is less of a problem if we want to synchronously execute a piece of code that uses Async IO module, as we only have to define an event loop where the code will run, then wait for it to end. With the specific case of vt-py, instead of trying to get the event loop and calling the VT API if it is not present, you can do this:
‘new_event_loop’ and ‘set_event_loop’ are internal functions available in asyncio module. This will generate a new process and will set it as the main event loop where vt-py can run tasks. Then you can work with vt-py Client as usual, as it will do the rest of the stuff needed for waiting asynchronous tasks to be completed before continuing executing your code. However, this is a quick hack for you to have a workaround while we release vt-py 0.5.4 (which will do this same thing behind the scenes, so you don’t have to worry about if your WSGI application, whatever it is, has set a main event loop or not).