RuntimeError: There is no current event loop in thread 'django-main-thread'
See original GitHub issueHi, I’m trying to integrate ib-insync with django using django-channels to get real time data from interactive brokers using a socket and using reqTickByTickData. But I’m struggling with the initial connection.
I wrote this line of code in services.py:
import json, nest_asyncio, asyncio
from ib_insync import *
ib = IB()
def connect_to_ib():
print("Starting IB")
ib_connection = ib.connect('127.0.0.1', 4002, clientId=1,timeout=4, readonly=False, account='')
print("IB is Connected?", ib.isConnected())
return ib_connection
but I’m getting this error:
RuntimeError: There is no current event loop in thread 'django-main-thread'
It worked in the past briefly and get an ib Object but I don’t know why It fails now.
I read #33 and #167 in order to solve this error but the problem persist.
def create_evenloop():
print("Starting Event Loop")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
nest_asyncio.apply()
Those functions were called in a consumer in django channels, (normally django is blocking but AsyncWebsocketConsumer from channels make non-blocking code inside PlotterConsumer class )
class PlotterConsumer(AsyncWebsocketConsumer):
create_evenloop()
ib = connect_to_ib()
...
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
RuntimeError: There is no current event loop in thread in ...
In your def demo_async(urls) , try to replace: loop = asyncio.get_event_loop(). with: loop = asyncio.new_event_loop() ...
Read more >How to fix Python asyncio RuntimeError: There is no current ...
You are trying to run asyncio.get_event_loop() in some thread other than the main thread – however, asyncio only generates an event loop for...
Read more >runtimeerror: there is no current event loop in thread 'thread-1'.
I want to run a asynchronous event in background with flask and for that i worte a code below But When I run...
Read more >Azure Function RuntimeError: There is no current event loop ...
Microsoft Q&A is the best place to get answers to all your technical questions on Microsoft products and services. Community. Forum.
Read more >Event Loop — Python 3.11.1 documentation
Return the running event loop in the current OS thread. Raise a RuntimeError if there is no running event loop. This function can...
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
no, I think goodboy has exaggerated the situation a bit in his response.
You can use ib_insync with any asyncio framework, but you should be careful to call async functions only, since all the sync-functions use the patching on the build-in python asyncio loop, which is obviously not working when you use non-native event loop. So, basic idea with any app which already has async-io capabilities - (1) start the app and it’s own event loop, (2) call all only “async def xxxAsync()” functions from ib_insync framework (from your app’s async functions)
The best place to look how it is done and ask additional questions is probably the ib-insync discussion group, which has plenty of posts on the subject https://groups.io/g/insync
@juan9222 was this ever resolved? I’m running into the same issue when trying to run my local server in django