Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INSTALLATION] - Can only run one Playwright at a time #341

Closed
RinSydney opened this issue Nov 11, 2020 · 13 comments
Closed

[INSTALLATION] - Can only run one Playwright at a time #341

RinSydney opened this issue Nov 11, 2020 · 13 comments
Labels
bug Something isn't working installation_help Someone is having problems installing the api

Comments

@RinSydney
Copy link

After pip installing TikTok-Api and trying to import for the first time I get the following error:

File "C:\Users\username\anaconda3\lib\site-packages\playwright\main.py", line 75, in run_driver
raise Error("Can only run one Playwright at a time.")

Error: Can only run one Playwright at a time.

The only code I am running in the basic import:

import TikTokApi
api = TikTokApi()

Desktop:

  • OS: [Windows 10]
  • TikTokApi 3.7.6
  • playwright 0.152.0
  • Tried using both Spyder and Juypter and I get the the same error message.
  • I looked through the closed issues, but I couldn't find the same error message there. Thank you in advance for any suggestions.
@RinSydney RinSydney added the installation_help Someone is having problems installing the api label Nov 11, 2020
@issue-label-bot issue-label-bot bot added the bug Something isn't working label Nov 11, 2020
@issue-label-bot
Copy link

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.74. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@davidteather
Copy link
Owner

Use api=TikTokApi.get_instance()

@RinSydney
Copy link
Author

Hi David - Thanks for the superquick response. Afraid I'm still getting the same error. Here is the full trace just from the simple import:

import TikTokApi
Traceback (most recent call last):

File "", line 1, in
import TikTokApi

File "C:\Users\username\anaconda3\lib\site-packages\TikTokApi_init_.py", line 1, in
from TikTokApi.tiktok import TikTokApi

File "C:\Users\username\anaconda3\lib\site-packages\TikTokApi\tiktok.py", line 6, in
from .browser import browser, get_playwright

File "C:\Users\username\anaconda3\lib\site-packages\TikTokApi\browser.py", line 16, in
raise e

File "C:\Users\username\anaconda3\lib\site-packages\TikTokApi\browser.py", line 14, in
playwright = sync_playwright().start()

File "C:\Users\username\anaconda3\lib\site-packages\playwright_init_.py", line 34, in sync_playwright
return SyncPlaywrightContextManager()

File "C:\Users\username\anaconda3\lib\site-packages\playwright\main.py", line 81, in init
self._connection = run_driver()

File "C:\Users\username\anaconda3\lib\site-packages\playwright\main.py", line 75, in run_driver
raise Error("Can only run one Playwright at a time.")

Error: Can only run one Playwright at a time.

@davidteather
Copy link
Owner

Can you provide the code you’re using?

@RinSydney
Copy link
Author

Literally all I am doing is the very basic install in a clean notebook with an upgrade request to check I have the latest version of both packages:

pip install TikTokApi --upgrade
pip install playwright -- upgrade

import TikTokApi

I haven't been able to get any further before the error is thrown.

@davidteather
Copy link
Owner

Probably has to do with Playwright Issue

@RinSydney
Copy link
Author

Ok thanks. Will try those solutions and update this for other users if any of them resolve it.

@adrennhoff
Copy link

Ok thanks. Will try those solutions and update this for other users if any of them resolve it.

Same problem here. I have explored the linked Playwright issue posted above but to no avail. I have attempted on both Mac and Windows.

@cesarblancg
Copy link

I tried this code:

from TikTokApi import TikTokApi
import threading

workers = []

def test():
  try:
    api=TikTokApi.get_instance()
  except Exception as e:
    print("Thread error")
    print(e)

for i in range(4):
    worker = threading.Thread(target=test)
    worker.start()
    workers.append(worker)

And have the same error: "cannot switch to a different thread".

Any ideas?

@dnobs
Copy link

dnobs commented Nov 20, 2020

I also received a similar error. I noticed that if I re-run this cell, the error goes away (only to be replaced by a different error: "AttributeError: 'TikTokApi' object has no attribute 'width')

Any help would be appreciated.

---------------------------------------------------------------------------
Error                                     Traceback (most recent call last)
<ipython-input-1-85f24666e59e> in <module>
      1 from TikTokApi import TikTokApi
----> 2 api = TikTokApi.get_instance()
      3 
      4 results = 10
      5 

~\anaconda3\lib\site-packages\TikTokApi\tiktok.py in get_instance(**kwargs)
     75     def get_instance(**kwargs):
     76         if not TikTokApi.__instance:
---> 77             TikTokApi(**kwargs)
     78         return TikTokApi.__instance
     79 

~\anaconda3\lib\site-packages\TikTokApi\tiktok.py in __init__(self, **kwargs)
     41         self.signer_url = kwargs.get("external_signer", None)
     42         if self.signer_url == None:
---> 43             self.browser = browser(**kwargs)
     44             self.userAgent = self.browser.userAgent
     45 

~\anaconda3\lib\site-packages\TikTokApi\browser.py in __init__(self, **kwargs)
     76             self.browser = get_playwright().webkit.launch(args=self.args, **self.options)
     77         except Exception as e:
---> 78             raise e
     79             logging.critical(e)
     80 

~\anaconda3\lib\site-packages\TikTokApi\browser.py in __init__(self, **kwargs)
     74 
     75         try:
---> 76             self.browser = get_playwright().webkit.launch(args=self.args, **self.options)
     77         except Exception as e:
     78             raise e

~\anaconda3\lib\site-packages\TikTokApi\browser.py in get_playwright()
     22             playwright = sync_playwright().start()
     23         except Exception as e:
---> 24             raise e
     25 
     26     return playwright

~\anaconda3\lib\site-packages\TikTokApi\browser.py in get_playwright()
     20     if playwright == None:
     21         try:
---> 22             playwright = sync_playwright().start()
     23         except Exception as e:
     24             raise e

~\anaconda3\lib\site-packages\playwright\__init__.py in sync_playwright()
     32 
     33 def sync_playwright() -> SyncPlaywrightContextManager:
---> 34     return SyncPlaywrightContextManager()
     35 
     36 

~\anaconda3\lib\site-packages\playwright\main.py in __init__(self)
     79 class SyncPlaywrightContextManager:
     80     def __init__(self) -> None:
---> 81         self._connection = run_driver()
     82         self._playwright: SyncPlaywright
     83 

~\anaconda3\lib\site-packages\playwright\main.py in run_driver()
     73     loop = asyncio.get_event_loop()
     74     if loop.is_running():
---> 75         raise Error("Can only run one Playwright at a time.")
     76     return loop.run_until_complete(run_driver_async())
     77 

Error: Can only run one Playwright at a time.

@mendeleden
Copy link

mendeleden commented Dec 7, 2020

from: microsoft/playwright-python#178 (comment)

Currently, playwright disallows attaching to a running loop:

https://github.com/microsoft/playwright-python/blob/d98d38b89b4864966218b1333c9399fc35487725/playwright/main.py#L79-L83

This should be fine in plain IPython (does not launch an asyncio event loop in the main thread), but will fail in Jupyter that uses tornado>=5 (launches asyncio event loop on startup under the hood).

As calling run_until_complete is disallowed on running loops, this will fail in Jupyter.

Minimal reproducible example that will run in latest IPython, but will break on Jupyter environments that use tornado>=5 under the hood:

import asyncio
print(asyncio.run(asyncio.sleep(0)))

So for Jupyter, additionally a pip install 'tornado<5' 'notebook<5.7.9' should fix loop issues introduced in tornado>=5 (and if you need jupyterhub, use jupyterhub<0.9.0). refs jupyter/notebook#3397 (comment) ipython/ipykernel#548

In the first note block run:
pip install 'tornado<5' 'notebook<5.7.9'
then run:

import nest_asyncio
nest_asyncio.apply()
from TikTokApi import TikTokApi
api = TikTokApi.get_instance()
results = 10
trending = api.trending(count=results, custom_verifyFp="")

@SpyderRivera
Copy link

SpyderRivera commented Dec 16, 2020

Using api = TikTokApi.get_instance(use_selenium=True) helped me!

@davidteather
Copy link
Owner

Closing due to inactivity open a new issue if you continue to have this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working installation_help Someone is having problems installing the api
Projects
None yet
Development

No branches or pull requests

7 participants