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

RuntimeError: This event loop is already running cause using jupyter as editor #30

Closed
Yukimagi opened this issue Oct 23, 2023 · 17 comments
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@Yukimagi
Copy link


RuntimeError Traceback (most recent call last)
Cell In[10], line 25
23 if name == "main":
24 loop = asyncio.get_event_loop()
---> 25 loop.run_until_complete(test_ask())

File ~\anaconda3\Lib\asyncio\base_events.py:629, in BaseEventLoop.run_until_complete(self, future)
618 """Run until the Future is done.
619
620 If the argument is a coroutine, it is wrapped in a Task.
(...)
626 Return the Future's result, or raise its exception.
627 """
628 self._check_closed()
--> 629 self._check_running()
631 new_task = not futures.isfuture(future)
632 future = tasks.ensure_future(future, loop=self)

File ~\anaconda3\Lib\asyncio\base_events.py:588, in BaseEventLoop._check_running(self)
586 def _check_running(self):
587 if self.is_running():
--> 588 raise RuntimeError('This event loop is already running')
589 if events._get_running_loop() is not None:
590 raise RuntimeError(
591 'Cannot run the event loop while another loop is running')

RuntimeError: This event loop is already running

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

你可以給我完整的程式碼嗎?
只有少數片段我會難以識別出錯誤是在哪裡發生的。
Please get me full code snippet.
I can't locate error use this exception message.

如果你是使用範例程式碼,請提供環境。
If you are using example, pls let me know your python env.

@Yukimagi
Copy link
Author

Yukimagi commented Oct 23, 2023

我是使用您提供的範例程式碼:(排版有點怪,不好意思)
我的版本是:Python 3.11.4

import asyncio
import json
from pathlib import Path

from re_edge_gpt import Chatbot
from re_edge_gpt import ConversationStyle

async def test_ask() -> None:
cookies = json.loads(open(
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
bot = await Chatbot.create(cookies=cookies)
response = await bot.ask(
prompt="find me some information about the new ai released by meta.",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
)
await bot.close()
print(json.dumps(response, indent=2))
assert response

if name == "main":
loop = asyncio.get_event_loop()
loop.run_until_complete(test_ask())

謝謝您的幫忙~

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

你把運行那段改成這樣呢?

if __name__ == "__main__":
    try:
        loop = asyncio.get_running_loop()
    except RuntimeError:
        loop = asyncio.get_event_loop()
    loop.run_until_complete(test_ask())

@Yukimagi
Copy link
Author

C:\Users\USER\anaconda3\Lib\re_init_.py:258: RuntimeWarning: coroutine 'test_ask' was never awaited
return pattern.translate(_special_chars_map)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback


RuntimeError Traceback (most recent call last)
Cell In[14], line 28
26 except RuntimeError:
27 loop = asyncio.get_event_loop()
---> 28 loop.run_until_complete(test_ask())

File ~\anaconda3\Lib\asyncio\base_events.py:629, in BaseEventLoop.run_until_complete(self, future)
618 """Run until the Future is done.
619
620 If the argument is a coroutine, it is wrapped in a Task.
(...)
626 Return the Future's result, or raise its exception.
627 """
628 self._check_closed()
--> 629 self._check_running()
631 new_task = not futures.isfuture(future)
632 future = tasks.ensure_future(future, loop=self)

File ~\anaconda3\Lib\asyncio\base_events.py:588, in BaseEventLoop._check_running(self)
586 def _check_running(self):
587 if self.is_running():
--> 588 raise RuntimeError('This event loop is already running')
589 if events._get_running_loop() is not None:
590 raise RuntimeError(
591 'Cannot run the event loop while another loop is running')

RuntimeError: This event loop is already running

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

我裝 anaconda 看看,說不定是他的問題。

@Yukimagi
Copy link
Author

了解,謝謝您,還是您可以跟我說您是用什麼跑的,或是環境?非常感謝您!

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

Pycharm 2023.1.4
Python 3.9 ~ 3.11 都有測試
套件抓 requirements 裡的全部最新

@Yukimagi
Copy link
Author

好的,謝謝您~

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

我剛注意到 conda 好像不能抓到 package 或許你可以用 VSCode 跟 原生 Python pip 試試看。

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

除了 Conda install 抓不到package外,我用 Anaconda3 python3.9 是可以跑得。

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

如果你是採用 Jupyter 要注意他原本就有啟用 asyncio,你可能要額外 安裝 nest-asyncio
並在開頭 nest_asyncio.apply()

@Yukimagi
Copy link
Author

Yukimagi commented Oct 23, 2023 via email

@Yukimagi
Copy link
Author

Yukimagi commented Oct 23, 2023 via email

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

import asyncio
import json
from pathlib import Path

from nest_asyncio import apply

from re_edge_gpt import Chatbot
from re_edge_gpt import ConversationStyle


async def test_ask() -> None:
    cookies = json.loads(open(
        str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
    bot = await Chatbot.create(cookies=cookies)
    response = await bot.ask(
        prompt="find me some information about the new ai released by meta.",
        conversation_style=ConversationStyle.balanced,
        simplify_response=True,
    )
    await bot.close()
    print(json.dumps(response, indent=2))
    assert response


if __name__ == "__main__":
    apply()
    try:
        loop = asyncio.get_running_loop()
    except RuntimeError:
        loop = asyncio.get_event_loop()
    loop.run_until_complete(test_ask())

試試看這個使用 nest_asyncio 的,要記得安裝 nest_asyncio 套件!

@Yukimagi
Copy link
Author

Yukimagi commented Oct 23, 2023 via email

@JE-Chen JE-Chen added documentation Improvements or additions to documentation question Further information is requested labels Oct 23, 2023
@Yukimagi
Copy link
Author

可以了~非常感謝您!

@JE-Chen
Copy link
Member

JE-Chen commented Oct 23, 2023

也謝謝你,之前沒注意到這個問題,之後會寫在 README 跟 文件給人參考如何解決。

@JE-Chen JE-Chen changed the title RuntimeError RuntimeError: This event loop is already running cause using jupyter as editor Oct 24, 2023
@JE-Chen JE-Chen closed this as completed Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants