English
Open in Telegram

ThreadMy one function do fetch a image's url form internet and saves in the ssd and do some editing using the pillow.

9 messages · –
N
My one function do fetch a image's url form internet and saves in the ssd and do some editing using the pillow. See When i will do code in fastapi's web dev should i try to write in async way, but here pillow is not allow to write async code, so what will the way, will i make simple def sync_fun() and time of call this function will i call it via asyncio.to_thread(sync_fun) will it fully ok to use like this in fastapi web app?
  1. R
    While downloading is I/O bound and belongs to a thread or async, pillow and/or ffmpeg are CPU-bound, and these below in their own proccess, not a thread
    1. N
      def get_collection(): myclient = pymongo.MongoClient("mongodb://localhost:27017/") mycol = myclient["mydatabase"]["customers"] mydoc = mycol.find().sort("name") return mydoc mycol = get_collection() See if i keep this logic of search a user and keep this in a function and use with my PTB library ot fastapi to get data of a telegram user so at that moment i call this connection to fetch the data and it take 500ms so for this 500 ms my Python tg bot will not respond to anyone so this time i will call this on asyncio.to_thread(get_collection()) so what should i do? is to_thread is ok here?
      1. M
        No, move MongoClient out of get_collection You need exactly one client per process believe me, for your tasks is enough
        1. N
          yaa it could be a good things, now after i will move client outside the fun, my cpu usage will low it will not recreatet the client all time right?
      2. R
        You did not even read my answer or tried to understand it... Btw, Mongo has async driver Why are you using PyMongo which is sync in async application?? It is called Motor - look it up
        1. N
          As of May 14, 2025, Motor is deprecated in favor of the GA release of the PyMongo Async API in the PyMongo library i think i should to use, from pymongo import AsyncMongoClient or maybe, Beanie - is an asynchronous Python object-document mapper (ODM) for MongoDB. Data models are based on Pydantic. But i am Using PTB repo there is i am using pydantic models but not fastapi directly, so there now what will my tool. As You say not to use asyncio.to_thread(sync_mongo_fun...), so which i will use now ?
          1. R
            No, to_thread should be used as a last resort (and when you know what you are doing) Use whatever you like, just do not use to_thread() if you have other (better) options. And learn the diffrences of async code vs using threads vs using proccesses. You cannot work with those until you understand what those are.
PPythonPython@Python · group · Tech
94 201members57writing in 30 days
Venue feed Open in Telegram

An open public feed from the search index ChatCrawler — “Google for public Telegram”; refreshed as the venue is crawled. Times are UTC.

Public content only, official Telegram API. About · FAQ · Index limits · Remove a page · Catalog · Search · How we count