Issue
I'm converting my normal views to async views due to request queries blocking all my threads. So far I've solved most of my problems except one. How to async save a model ?
async def dashboardAddChart(request, rowId):
row = (await sync_to_async(list)(DashboardRow.objects.filter(pk=rowId).select_related('dashboard__site', 'dashboard__theme')))[0]
chart = DashboardChart(dashboard=row.dashboard, dashboardRow=row)
if row.dashboard.theme is not None:
dashboardThemes.applyThemeToChart(chart)
chart.save()
chartData = await getChartData(chart.pk)
I've tried numerous things with chart.save() including:
await sync_to_async(chart.save)
t = asyncio.ensure_future(sync_to_async(chart.save))
await asyncio.gather(t)
But I'm not getting it right.
Any help will be appreciated!
Solution
await sync_to_async(chart.save)
should be
await sync_to_async(chart.save)()
The sync_to_async
function wraps chart.save
and returns an asynchronous version of it, which then still has to be called to execute.
Answered By - user2842312
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.