I have a discord bot that I suspect is having periodic issues due to occasional slow pycurl calls. After some research I found out pycurl is not asynchronous, and likely the cause for my troubles.
I have this function:
def communicate_wallet(wallet_command):
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, '[::1]')
c.setopt(c.PORT, 7076)
c.setopt(c.POSTFIELDS, json.dumps(wallet_command))
c.setopt(c.WRITEFUNCTION, buffer.write)
c.perform()
c.close()
body = buffer.getvalue()
parsed_json = json.loads(body.decode('iso-8859-1'))
return parsed_json
This is equivalent to a curl command like:
curl -g -d '{ "action": "action_def" }' '[::1]:7076'
I'm wondering if there's an async alternative to do this, so i can call communicate_wallet with await. I couldn't seem to find any asynchronous-compatible alternatives to pycurl.
Thanks
I'm wondering if there's an async alternative to do this, so i can call communicate_wallet with await.
The simplest option is to use run_in_executor for the blocking code:
loop = asyncio.get_event_loop()
data = await loop.run_in_executor(None, communicate_wallet, wallet_command)
This will submit the blocking function to a thread pool and awaken your coroutine when complete, allowing asyncio to go about its business in the meantime.
A better way is to replace pycurl with an http client that natively supports asyncio, such as aiohttp. This will take more work initially, but might pay off in the long run because it will allow the http code to communicate with the tasks run by asyncio without thread synchronization.
The Tornado package seems to have what you want in the form of tornado.curl_httpclient.CurlAsyncHTTPClient.
Related
I am trying to write an API client for Telegram using Telethon.
https://github.com/LonamiWebs/Telethon
If you create a TelegramClient(session) it prompts for input upon initialization if your session isn’t authorized.
This is great when manually running the program from the terminal, but what if I want to run it inside a daemon or cron job?
They are using the default Input method from python3 to gather the input. I don’t see any way in the library to specify a session file and check if it’s logged in that can be run before initializing a TelegramClient, and it’s the initializer that will prompt for input if not logged in.
This feels like a catch 22! Does anybody know if this might produce an error that could be caught? Or what happens when input() is run with no tty? Would it just hang? Could I add a timeout in that case?
Thanks in advance for helping me understand better!
You are affirming that the initialization of TelegramClient invokes the input function as default, but this is done inside the TelegramClient.start method (docs).
Taking the solution that you give at the end of your question is a fair aproach, so let's use a timeout when invoking input.
from asyncio import get_event_loop, wait_for, TimeoutError
from functools import partial
from telethon import TelegramClient
async def ainput(prompt):
"""Reads input from stdin in an async way"""
loop = get_event_loop()
await loop.run_in_executor(None, print, prompt)
return await loop.run_in_executor(None, input)
async def get_code(timeout):
"""Waits for the code from stdin with a timeout"""
try:
return await wait_for(
ainput("Please, type the code you received: "),
timeout=timeout
)
except TimeoutError:
pass
client = TelegramClient(session, api_id, api_hash).start(
phone=phone,
code_callback=partial(get_code, 30)
)
You should keep in mind that when you call start the arguments phone, and password also reads from stdin if it isn't provided a callable or default value, so you can handle them like in this example with code_callback.
In your case you can get the code from a POST to your API or in other way, just get creative and write the callable that fits your needs.
The telegram bot I'm making can execute a function that takes a few minutes to process and I'd like to be able to continue to use the bot while it's processing the function.
I'm using aiogram, asyncio and I tried using Python threading to make this possible.
The code I currently have is:
import asyncio
from queue import Queue
from threading import Thread
import time
import logging
from aiogram import Bot, types
from aiogram.types.message import ContentType
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import Dispatcher, FSMContext
from aiogram.utils.executor import start_webhook
from aiogram.types import InputFile
...
loop = asyncio.get_event_loop()
bot = Bot(token=BOT_TOKEN, loop=loop)
dp = Dispatcher(bot, storage=MemoryStorage())
dp.middleware.setup(LoggingMiddleware())
task_queue = Queue()
...
async def send_result(id):
logging.warning("entered send_result function")
image_res = InputFile(path_or_bytesio="images/result/res.jpg")
await bot.send_photo(id, image_res, FINISHED_MESSAGE)
def queue_processing():
while True:
if not task_queue.empty():
task = task_queue.get()
if task["type"] == "nst":
nst.run(task["style"], task["content"])
send_fut = asyncio.run_coroutine_threadsafe(send_result(task['id']), loop)
send_fut.result()
task_queue.task_done()
time.sleep(2)
if __name__ == "__main__":
executor_images = Thread(target=queue_processing, args=())
executor_images.start()
start_webhook(
dispatcher=dp,
webhook_path=WEBHOOK_PATH,
skip_updates=False,
on_startup=on_startup,
host=WEBAPP_HOST,
port=WEBAPP_PORT,
)
So I'm trying to setup a separate thread that's running a loop that is processing a queue of slow tasks thus allowing to continue chatting with the bot in the meantime and which would send the result message (image) to the chat after it's finished with a task.
However, this doesn't work. My friend came up with this solution while doing a similar task about a year ago, and it does work in his bot, but it doesn't seem to work in mine.
Judging by logs, it never even enters the send_result function, because the warning never comes through. The second thread does work properly though and the result image is saved and is located in its assigned path by the time nst.run finishes working.
I tried A LOT of different things and I'm very puzzled why this solution doesn't work for me because it does work with another bot. For example, I tried using asyncio.create_task instead of asyncio.run_coroutine_threadsafe, but to no avail.
To my understanding, you don't need to pass a loop to aiogram's Bot or Dispatcher anymore, but in that case I don't know how to send a task to the main thread from the second one.
Versions I'm using: aiogram 2.18, asyncio 3.4.3, Python 3.9.10.
Solved, the issue was that you can't access the bot's loop directly (with bot.loop or dp.loop) even if you pass your own asyncio loop to the bot or the dispatcher.
So the solution was to access the main thread's loop by using asyncio.get_event_loop() (which returns currently running loop, if there's one) from within one of the message handlers, because the loop is running at this point, and pass it to asyncio.run_coroutine_threadsafe (I used the "task" dictionary for that) like this: asyncio.run_coroutine_threadsafe(send_result(task['id']), task['loop']).
In the python asyncio websockets library, the example calls run_forever(). Why is this required?
Shouldn't run_until_complete() block and run the websockets loop?
#!/usr/bin/env python
# WS server example
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
start_server = websockets.serve(hello, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
# if you comment out the above line, this doesn't work, i.e., the server
# doesn't actually block waiting for data...
If I comment out run_forever(), the program ends immediately.
start_server is an awaitable returned by the library. Why isn't run_until_complete sufficient to cause it to block/await on hello()?
websockets.serve simply starts the server and exits immediately. (It still needs to be awaited because configuring the server can require network communication.) Because of that, you need to actually run the event loop.
Since the server is designed to run indefinitely, you cannot run the event loop in the usual way, by passing a coroutine to run_until_complete. As the server has already started, there is no coroutine to run, you just need to let the event loop run and do its job. This is where run_forever comes in useful - it tells the event loop to run (executing the tasks previously scheduled, such as those belonging to the server) indefinitely, or until told to stop by a call to loop.stop.
In Python 3.7 and later one should use asyncio.run to run asyncio code, which will create a new event loop, so the above trick won't work. A good way to accomplish the above in modern asyncio code would be to use the serve_forever method (untested):
async def my_server():
ws_server = await websockets.serve(hello, "localhost", 8765)
await ws_server.server.serve_forever()
asyncio.run(my_server())
When writing asynchronous crawlers using asyncio and aiohttp in Python, I have always had a question: why you must use async with, and it's easy to report errors if you don't use them.
Although aiohttp also has a method request, it can support calling a simpler api. I want to know what is the difference. I still like the requests module very much, I don't know if it can be used as simple as the requests module.
why you must use async with
It's not like you must use async with, it's just a fail-safe device for ensuring that the resources get cleaned up. Taking a classic example from the documentation:
async def fetch(session, url):
async with session.get(url) as response:
return await response.text()
You can re-write it as:
async def fetch(session, url):
response = await session.get(url)
return await response.text()
This version appears to work the same, but it doesn't close the response object, so some OS resources (e.g. the underlying connection) may continue to be held indefinitely. A more correct version would look like this:
async def fetch(session, url):
response = await session.get(url)
content = await response.text()
response.close()
return content
This version would still fail to close the response if an exception gets raised while reading the text. It could be fixed by using finally - which is exactly what with and async with do under the hood. With an async with block the code is more robust because the language makes sure that the cleanup code is invoked whenever execution leaves the block.
I am currently writing a small flask-based micro-service which launches other python scripts via calls to a CLI using python's subprocess module. My ultimate goal is make a non-blocking async function call triggered by http requests to a route in the service and have the service return 200 response from the route while the async function runs in the background.
I have been perusing the docs (I am using Python 3.6.3 for this service) cannot work out how to achieve this. Here is a small example of how my code is structured:
#app.route('/execute_job')
def execute_job():
params = ...
run_async_job(params)
return 'Launched async job according to params, it is now running.'
async def run_async_job(params):
command = 'run_python_cli_scripts args'
proc = subprocess.Popen(command)
# change some envs, do some file io, yada yada yada
...
while True:
if proc.poll() is not None: # the cli script is finished
return notify_external_api_job_complete()
I know that simply calling run_async_job(params) does not actually begin its execution, but instead returns an awaitable or Task which must been thrown in an event_loop. My issue is that I cannot figure out how to run this task in an event_loop such that the return in execute_ job is reached before it completes. Is this sort of thing possible? This is my first foray into async python, and I am looking for behaviour similar to what you would see in async javascript. Is trying to use async def for the function I want to be non-blocking the wrong approach or is there a way to launch the tasks in an event_loop in a non-blocking fashion so that the aforementioned return 'Launched async job according to params, it is now running.' can be reached and the function completed before run_async_job(params) completes?
Thanks in advance for your time and wisdom.
Fwiw to posterity: I opted for using a child process launched via the subprocess module. This was achievable by converting the library file I imported my async def'd function from into a script which uses command line arguments parsed from the argparse module. My route now looks like
#app.route('/execute_job')
def execute_job():
params = ...
command = ('python', params)
subprocess.Popen(command)
return 'Launched async job according to params, it is now running.'
edit: formatting