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

There is no current event loop in thread 'Thread-1' #77

Open
autn opened this issue Oct 17, 2024 · 11 comments
Open

There is no current event loop in thread 'Thread-1' #77

autn opened this issue Oct 17, 2024 · 11 comments

Comments

@autn
Copy link

autn commented Oct 17, 2024

Hi, I'm using Zyte API for my Django project, but when I use Zyte, I get There is no current event loop in thread 'xxx' error.

My code:

from zyte_api import ZyteAPI

def omnifollow_product_extract(product_url):
    zyte_api_key = os.environ.get("ZYTE_API_KEY", "")
    client = ZyteAPI(api_key=zyte_api_key)
@Gallaecio
Copy link
Contributor

Could you provide instructions to generate a minimal, reproducible example?

@autn
Copy link
Author

autn commented Oct 23, 2024

@Gallaecio let me provide an elementary example:
On my Django project:

  • my_app/utils.py
from zyte_api import ZyteAPI

def omnifollow_product_extract(product_url):
    zyte_api_key = os.environ.get("ZYTE_API_KEY", "")  # or just set a static key
    client = ZyteAPI(api_key=zyte_api_key)
  • my_app/urls.py
urlpatterns = [
    path("zyte-test/", views.zyte_test),
    ...
]
  • my_app/views.py
from my_app.utils import omnifollow_product_extract

def zyte_test(request):
    omnifollow_product_extract("")
    return HttpResponse("success")

It's a basic way to use Zyte API? When I go to the view http://my_host/zyte-test/ I got this:
image

It looks like it's related to AsyncZyteAPI, but I want to use ZyteAPI only for my helper function.

@Gallaecio
Copy link
Contributor

Gallaecio commented Oct 24, 2024

ZyteAPI uses AsyncZyteAPI underneath. Could it be that you are creating ZyteAPI in one thread and then using it from a different thread? It’s possible we did not account for that scenario (and we should).

In any case, I will try to reproduce this when I get a chance.

@Gallaecio
Copy link
Contributor

I have tried reproducing your issue with this code base, but it seems to work. Could you create a PR against that repo with the necessary changes to reproduce your issue?

@lopuhin
Copy link

lopuhin commented Oct 24, 2024

@autn does this happen when you do ./manage.py runserver or when you run the Django app in some other way?

@Gallaecio
Copy link
Contributor

Oh, right, I used runserver.

@autn
Copy link
Author

autn commented Oct 24, 2024

@lopuhin I used runserver
@Gallaecio thank you, I'll update the repo

@autn
Copy link
Author

autn commented Oct 24, 2024

@Gallaecio looks like the issue happened on Python 3.8 and Django 2.2.19
Updated: I tried using Django 4.2 (the latest version supports Python 3.8) and got the same issue.

@autn
Copy link
Author

autn commented Oct 28, 2024

@Gallaecio, I re-read the README and found that Python 3.9+ is required. However, I can install it on my old project, which uses Python 3.8, without the error/warning. Should we update the Python dependence to the library?

@Gallaecio
Copy link
Contributor

You are right, we should probably add python_requires=">=3.9" to setup.py, since it seems at least 1 change in #76 could break stuff when using Python 3.8 or lower (if also using typing).

@autn
Copy link
Author

autn commented Oct 31, 2024

@Gallaecio thanks for your support 💯 I will update my project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants