'There is no current event loop in thread 'uWSGIWorker1Core13'

We are running into a bug with one of our API endpoints that handles fetching tiktok user info. Our app is written in Django and is using the Django rest framework. We are using uwsgi to serve our app.

We are able to serve our app locally using uwsgi and our tiktok API endpoint works as intended. However, when building and deploying our docker container to our production server, we see the following error response when making a request to our tiktok API endpoint:

There is no current event loop in thread 'uWSGIWorker1Core13'.

It's also important to point out that we are running our app in a synchronous environment. The PyTitktokApi has some async parts. We are thinking the the bug is related to this. It should be possible to run async code in a sync environment.

Here is some sample code of our API endpoint and the function responsible for fetching tiktok user info:

# rest API call
 
class TikTokConnectView(GenericAPIView):
  """
  Connect TikTok App View
  """
  serializer_class = TikTokConnectSerializer
 
  def post(self, request, *args, **kwargs):
    serializer = self.get_serializer(data=request.data)
    serializer.is_valid(raise_exception=True)
    access_token = serializer.validated_data['access_token']
    open_id = serializer.validated_data['open_id']
    response_status, username = tiktok_username(open_id, access_token)
    if not response_status:
      return Response({'detail': 'Error fetching username'}, status=status.HTTP_404_NOT_FOUND)
 
    # response_status, info = asyncio.run(tiktok_user_info(username))
    response_status, info = tiktok_user_info(username)
    if not response_status:
      return Response({'detail': 'Error fetching user info'}, status=status.HTTP_404_NOT_FOUND)
    ...
 
# TiktokAPI Handler
# def _tiktok_user_info(username):
def tiktok_user_info(username):
  verify_fp = "verify_kqtm6ec5_O0nquldq_6nlz_40yf_8aQB_6haj2dd6Fqi1"
  with TikTokApi(custom_verify_fp=verify_fp) as api:
    user = api.user(username=username)
    info = user.info_full()
  return True, info
# tiktok_user_info = sync_to_async(_tiktok_user_info, thread_sensitive=True)
 
# Command for running our django app using uwsqi
uwsgi -i uwsgi.ini --processes 4 --threads 20

Here is the link to the tiktok lib responsible for fetching tiktok user info: https://github.com/davidteather/TikTok-Api



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source