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
Right now, the Task returned by asyncio.ensure_future() in avanza_socket.init() is not stored or handled by the API. A reference to the Task must be maintained for as long as the Task is active, or else the Task gets garbage collected at some random time. You also need the reference to be able to either await or asyncio.gather() the Task so you can catch any exception from the connection (e.g. ConnectionClosedError).
You further need to regularly access Avanza within the account's timeout period to keep the login alive (you can select the timeout in your account's settings on Avanza).
Since the Task is not returned from the API right now, I have implemented a bad kludge as a temporary workaround. I use asyncio.all_tasks() to get all Tasks in the process and dig into the Task's internals (works in Python 3.11) to guess which one is the right one. I have a separate loop where I call avanza.get_position() and an asyncio.sleep() to do the keep-alive. So this is my code right now:
task_list = asyncio.all_tasks()
avanza_coro=None
for coro in task_list:
if coro.get_stack()[0].f_code.co_name == "__create_socket":
avanza_coro=coro
break
keep_alive_future = asyncio.create_task(keep_alive(avanza))
task_results=await asyncio.gather(avanza_coro, keep_alive_future)
I have a try-catch around the whole thing and recreate the Avanza connection whenever I get an exception.
It would be good if the API would give us the Task properly so I could make that code a bit nicer.
The text was updated successfully, but these errors were encountered:
What code changes would be needed for this to work?
Is just returning the task from ensure_future enough for it to work, or do you see a better way to structure it?
Right now, the Task returned by
asyncio.ensure_future()
inavanza_socket.init()
is not stored or handled by the API. A reference to the Task must be maintained for as long as the Task is active, or else the Task gets garbage collected at some random time. You also need the reference to be able to eitherawait
orasyncio.gather()
the Task so you can catch any exception from the connection (e.g.ConnectionClosedError
).You further need to regularly access Avanza within the account's timeout period to keep the login alive (you can select the timeout in your account's settings on Avanza).
Since the Task is not returned from the API right now, I have implemented a bad kludge as a temporary workaround. I use
asyncio.all_tasks()
to get all Tasks in the process and dig into the Task's internals (works in Python 3.11) to guess which one is the right one. I have a separate loop where I callavanza.get_position()
and anasyncio.sleep()
to do the keep-alive. So this is my code right now:I have a try-catch around the whole thing and recreate the Avanza connection whenever I get an exception.
It would be good if the API would give us the Task properly so I could make that code a bit nicer.
The text was updated successfully, but these errors were encountered: