'Python TikTokApi SynchronousOnlyOperation error
I have a Django project where I am trying to fetch TikTok user info from a class based django view. I am running into an issue that appears to be related to async/sync/threading.
Related documents: https://docs.djangoproject.com/en/3.2/topics/async/
I tried decorating views described in the link above still no luck. I added this bit of code to my gunicorn wsgi file os.environ.setdefault("DJANGO_ALLOW_ASYNC_UNSAFE", "true") which does fix the problem but seems like a bad idea.
Here is the bit of code that I am using to call the TikTokAPI:
def get_user_info(username):
verify_fp = "code here"
try:
api = TikTokApi(custom_verify_fp=verify_fp)
user = api.user(username=username)
info = user.info_full()
return info
except Exception:
return None
class TikTokAccount(APIView):
def post(self, request, *args, **kwargs):
...
info = get_user_info(username)
...
Traceback (most recent call last):
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/gevent/pywsgi.py", line 999, in handle_one_response
self.run_application()
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/gevent/pywsgi.py", line 951, in run_application
close()
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/django/http/response.py", line 259, in close
signals.request_finished.send(sender=self._handler_class)
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in send
return [
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/django/db/__init__.py", line 57, in close_old_connections
conn.close_if_unusable_or_obsolete()
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 510, in close_if_unusable_or_obsolete
if self.get_autocommit() != self.settings_dict['AUTOCOMMIT']:
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/django/db/backends/base/base.py", line 389, in get_autocommit
self.ensure_connection()
File "/projects/social-bean/.pyenv/lib/python3.9/site-packages/django/utils/asyncio.py", line 24, in inner
raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.
2022-02-26T02:14:04Z {'REMOTE_ADDR': '::1', 'REMOTE_PORT': '50871', 'HTTP_HOST': 'localhost:8080', (hidden keys: 27)} failed with SynchronousOnlyOperation
Python3.9 / Django 3.2 OS: MacOS / Ubuntu TikTokApi Version [5.0.0]
Any ideas on how to resolve?
Solution 1:[1]
I haven't tried out my TikTokApi package on Django, but yeah it's got some threading issues right now which I'm planning to fix hopefully sometime in the next two weeks or so.
In the meantime, someone made a PR that might help you out with the threading issues https://github.com/davidteather/TikTok-Api/pull/846 the syntax changed in that PR to using a with statement. I'm going to try to make it an optional with statement when that PR gets merged into master eventually.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Teather |
