Last active
November 22, 2019 13:34
-
-
Save Bigomby/9d52bd4d2737e6d3726ba1532bfeba9b to your computer and use it in GitHub Desktop.
Python blocking calls in async code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import twitter | |
import asyncio | |
import time | |
CONSUMER_KEY = '' | |
CONSUMER_SECRET = '' | |
ACCESS_TOKEN_KEY = '' | |
ACCESS_TOKEN_SECRET = '' | |
def getTweetsBlocking(api, term): | |
""" | |
Esta sería la llamada normal (bloqueante) a la API de Twitter | |
""" | |
print(f"Search started for {term}") | |
tweets = api.GetSearch(raw_query=f"q={term}&count=500") | |
print(f"Search done for {term}") | |
async def getTweets(api, term): | |
""" | |
En esta función asíncrona ejecutamos la función bloqueante (getTweetsBlocking) | |
dentro de un "executor", de forma que esta función (getTweets) no es bloqueante. | |
""" | |
loop = asyncio.get_event_loop() | |
await loop.run_in_executor(None, getTweetsBlocking, api, term) | |
async def main(): | |
api = twitter.Api( | |
consumer_key=CONSUMER_KEY, | |
consumer_secret=CONSUMER_SECRET, | |
access_token_key=ACCESS_TOKEN_KEY, | |
access_token_secret=ACCESS_TOKEN_SECRET, | |
) | |
# El método "gather" ejecuta de forma concurrente todas las funciones | |
# asíncronas que le pasemos y termina cuando todas hayan terminado. | |
await asyncio.gather( | |
getTweets(api, "Madrid"), | |
getTweets(api, "Barcelona"), | |
getTweets(api, "Valencia"), | |
getTweets(api, "Sevilla"), | |
) | |
print("All searchs finished") | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment