Python 异步函数中的 for 循环慢
Python slow for-loop in async func
我正在尝试 Song-Objects
到我的 Queue-Object
,以从库 Pafy
.[=26= 中循环播放 Playlist-Object
中的实际歌曲]
playlist = pafy.get_playlist("playlist url here")
counter = 0
for s in playlist["items"]:
counter += 1
s = s["pafy"]
song = Song.Song(s.watchv_url, msg.author.name, s.title, s.description, s.author, s.published, s.duration,
s.likes, s.dislikes,
s.viewcount, s.thumb)
if queue.add_song(song=song):
print("Song " + str(counter) + " finished")
else:
print("Song " + str(counter) + " not added")
class Song:
def __init__(self, url, requested_by, title, description, uploader, upload_date, duration, likes, dislikes, views, thumbnail):
self.url = url
self.requested_by = requested_by
self.title = title
self.description = description
self.uploader = uploader
self.upload_date = upload_date
self.duration = duration
self.likes = likes
self.dislikes = dislikes
self.views = views
self.thumbnail = thumbnail
class PlayerQueue:
list_of_songs = []
length = 0
current_song = []
replaying = False
random = False
def add_song(self, song):
try:
self.length += 1
self.list_of_songs.append(song)
return True
except Exception:
return False
def remove_song(self, song):
if song in self.list_of_songs:
self.length -= 1
self.list_of_songs.remove(song)
return True
else:
return False
def replay(self):
self.replaying = True
def randomize(self):
self.random = True
def clear(self):
self.list_of_songs = []
self.length = 0
self.replaying = False
self.random = False
我是否也应该使 add_song-Method
异步?
循环一次此代码大约需要 1-2 秒。
这导致我遇到 asyncio
的问题,因为它会抛出 TimeoutError
。此错误在 30 秒后发生,对于 70 首歌曲,循环需要一分多钟。这个循环这么慢,因为它是 async def function
中的 运行 吗?我可以让它更快吗?
错误如下:
ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<VoiceClient.poll_voice_ws() done, defined
at C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\voice_client.py:269> exception=TimeoutError()>
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 239, in _step
result = coro.send(None)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\voice_client.py", line 276, in poll_voice_ws
yield from self.ws.poll_event()
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\gateway.py", line 676, in poll_event
msg = yield from asyncio.wait_for(self.recv(), timeout=30.0, loop=self.loop)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 396, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError
我可以在现有 asyncio.event.loop
中为我的 for 循环创建一个新任务,这样我就不会 运行 进入这个 TimeoutError
吗?我应该试着抓住它然后继续吗?
目前运行宁:
Intel I5 系列CPU
64 GB DDR4 内存
Python 3.x
我现在正在使用另一个库来下载播放列表。我正在使用 thread library
到 运行,所以在加入 vc 之前,我在一个单独的线程中开始下载,所以我没有得到 timeout error
。
我正在尝试 Song-Objects
到我的 Queue-Object
,以从库 Pafy
.[=26= 中循环播放 Playlist-Object
中的实际歌曲]
playlist = pafy.get_playlist("playlist url here")
counter = 0
for s in playlist["items"]:
counter += 1
s = s["pafy"]
song = Song.Song(s.watchv_url, msg.author.name, s.title, s.description, s.author, s.published, s.duration,
s.likes, s.dislikes,
s.viewcount, s.thumb)
if queue.add_song(song=song):
print("Song " + str(counter) + " finished")
else:
print("Song " + str(counter) + " not added")
class Song:
def __init__(self, url, requested_by, title, description, uploader, upload_date, duration, likes, dislikes, views, thumbnail):
self.url = url
self.requested_by = requested_by
self.title = title
self.description = description
self.uploader = uploader
self.upload_date = upload_date
self.duration = duration
self.likes = likes
self.dislikes = dislikes
self.views = views
self.thumbnail = thumbnail
class PlayerQueue:
list_of_songs = []
length = 0
current_song = []
replaying = False
random = False
def add_song(self, song):
try:
self.length += 1
self.list_of_songs.append(song)
return True
except Exception:
return False
def remove_song(self, song):
if song in self.list_of_songs:
self.length -= 1
self.list_of_songs.remove(song)
return True
else:
return False
def replay(self):
self.replaying = True
def randomize(self):
self.random = True
def clear(self):
self.list_of_songs = []
self.length = 0
self.replaying = False
self.random = False
我是否也应该使 add_song-Method
异步?
循环一次此代码大约需要 1-2 秒。
这导致我遇到 asyncio
的问题,因为它会抛出 TimeoutError
。此错误在 30 秒后发生,对于 70 首歌曲,循环需要一分多钟。这个循环这么慢,因为它是 async def function
中的 运行 吗?我可以让它更快吗?
错误如下:
ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<VoiceClient.poll_voice_ws() done, defined
at C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\voice_client.py:269> exception=TimeoutError()>
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 239, in _step
result = coro.send(None)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\voice_client.py", line 276, in poll_voice_ws
yield from self.ws.poll_event()
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\site-packages\discord\gateway.py", line 676, in poll_event
msg = yield from asyncio.wait_for(self.recv(), timeout=30.0, loop=self.loop)
File "C:\Users\user\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 396, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError
我可以在现有 asyncio.event.loop
中为我的 for 循环创建一个新任务,这样我就不会 运行 进入这个 TimeoutError
吗?我应该试着抓住它然后继续吗?
目前运行宁:
Intel I5 系列CPU
64 GB DDR4 内存
Python 3.x
我现在正在使用另一个库来下载播放列表。我正在使用 thread library
到 运行,所以在加入 vc 之前,我在一个单独的线程中开始下载,所以我没有得到 timeout error
。