dynamicpdf-api / python-client-examples

DynamicPDF API Python client examples project.
0 stars 0 forks source link

Getting Python Error "This event loop is already running" #1

Open kjprice opened 2 months ago

kjprice commented 2 months ago

I am running this example https://github.com/dynamicpdf-api/python-client-examples/blob/d3744b65740d0a07ea7970597241d51fe8f2953a/PdfTextExample.py

I am using google collab to run the program.

Here is the exact code I am running:

from dynamicpdf_api.pdf_text import PdfText
from dynamicpdf_api.pdf_resource import PdfResource
resource = PdfResource(PDF_FILEPATH)
pdf_text = PdfText(resource)
pdf_text.api_key = DPDF_API_KEY
pdf_text.start_page=1
pdf_text.page_count=2
response = pdf_text.process()
print(response.json_content)

I am getting back an error:

RuntimeError                              Traceback (most recent call last)
[<ipython-input-95-5ea014e86660>](https://localhost:8080/#) in <cell line: 11>()
      9 pdf_text.start_page=1
     10 pdf_text.page_count=2
---> 11 response = pdf_text.process()
     12 print(response.json_content)

2 frames
[/usr/lib/python3.10/asyncio/base_events.py](https://localhost:8080/#) in _check_running(self)
    582     def _check_running(self):
    583         if self.is_running():
--> 584             raise RuntimeError('This event loop is already running')
    585         if events._get_running_loop() is not None:
    586             raise RuntimeError(

RuntimeError: This event loop is already running
kjprice commented 2 months ago

Note that I was able to fix this issue by running the following beforehand:

! pip install -q nest-asyncio
import nest_asyncio
nest_asyncio.apply()

This shouldn't have to be done and hopefully it gets fixed. But this should help others facing this issue. I assume this issue only exists in google collab (or jupyter notebooks in general).