davidteather / TikTok-Api

The Unofficial TikTok API Wrapper In Python
https://davidteather.github.io/TikTok-Api
MIT License
4.7k stars 952 forks source link

[BUG] - greenlet.error: cannot switch to a different thread #379

Closed handole closed 3 years ago

handole commented 3 years ago

I tried to combine into django with this TikTokApi, before that there was no problem when I was using version 3.7.5 it worked smoothly.

However, after I upgraded the version this error occurred

  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/rest_framework/decorators.py", line 50, in handler
    return func(*args, **kwargs)
  File "/home/handoko/DEV/EVELLO/evello-scrapper/tiktok/views.py", line 113, in by_trending
    trending = api.trending(n_post)
  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/TikTokApi/tiktok.py", line 256, in trending
    res = self.getData(url=api_url, **kwargs)
  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/TikTokApi/tiktok.py", line 136, in getData
    verify_fp, did, signature = self.browser.sign_url(**kwargs)
  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/TikTokApi/browser.py", line 162, in sign_url
    page = self.create_page()
  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/TikTokApi/browser.py", line 110, in create_page
    context = self.browser.newContext(**iphone)
  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/playwright/sync_api.py", line 6188, in newContext
    self._sync(
  File "/home/handoko/DEV/EVELLO/ven/lib/python3.8/site-packages/playwright/sync_base.py", line 105, in _sync
    dispatcher_fiber_.switch()
greenlet.error: cannot switch to a different thread

I tried to return to the previous version too, but it still got an error. hoping for an answer

davidteather commented 3 years ago

Try to add this to the top of your code

from gevent import monkey
monkey.patch_all()
davidteather commented 3 years ago

Closing because stale, feel free to comment on this issue if it's not fixed and I'll probably remember to reopen it

ranahaani commented 3 years ago

@davidteather not fixed yet

lau1944 commented 3 years ago

I initialize TikTokApi inside the function instead of outside, it works fine

def xxxfun :
   api = TikTokApi.get_instance()
   api.trend()
Auth0rM0rgan commented 3 years ago

not working for me as well. any solution?