joetats / youtube_search

Tool for searching for youtube videos to avoid using their heavily rate-limited API
MIT License
220 stars 64 forks source link

asynchronous support? #27

Open WOLFIE-OG opened 3 years ago

WOLFIE-OG commented 3 years ago

I am including the search in my discord bot, and it seems to freeze the bot while it searches if setting == 'url': url=query elif setting == 'search': yt = YoutubeSearch(query, max_results=1).to_json() yt_id = str(json.loads(yt)['videos'][0]['id']) url = 'https://www.youtube.com/watch?v='+yt_id

joetats commented 3 years ago

It does take some time, unfortunately I'm not too familiar with async in Python. Are you able to use multiprocessing or something to let the bot create a new thread to perform the search and then do something with the results once they come in?

WOLFIE-OG commented 3 years ago

It does take some time, unfortunately I'm not too familiar with async in Python. Are you able to use multiprocessing or something to let the bot create a new thread to perform the search and then do something with the results once they come in?

I can make it into a async subprocess but am i able to run it as a command from the command line then pass the out put?

ldo commented 1 year ago

Seems that requests doesn’t do async. An obvious way to turn blocking calls into nonblocking ones is to push them off into a separate worker thread, and feed it requests via a Queue. When a request has completed, the thread can return its result to a caller which is awaiting a Future by putting the result into the future via call_soon_threadsafe.