Недавно делал быстрый прототип асинхронного приложения в котором требовалось вызывать много синхронного кода. Да, я знаю, что это не лучший дизайн, но нужно было быстрое решение на один процесс и без очередей. Поэтому я выполнял код в потоках.
Выглядело это примерно так:
from fastapi.concurrency import run_in_threadpool
async def execute(data: DataRequest) -> DataResponse:
try:
result = await run_in_threadpool(sync_function, data)
return DataResponse(data=result)
except Exception as e:
return DataResponse(
error=str(e),
success=False,
)
В общем работает нормально. Для всех вызовов под капотом используется общий тредпул, всё работает предсказуемо.
Но потребовалось изменить количество запускаемых в пуле потоков (по умолчанию создается 40 воркеров).
Так как дело происходит с FastAPI, делается это через lifespan используя настройки anyio:
import anyio
@asynccontextmanager
async def lifespan(app: FastAPI):
limiter = anyio.to_thread.current_default_thread_limiter()
limiter.total_tokens = 100
yield
# если вдруг нужно вернуть обратно
limiter.total_tokens = 40
Зачем менять количество воркеров?
- уменьшить, если оперативки мало (один тред занимает ~8мб)
- увеличить чтобы выдержать нагрузку
Если есть предложения получше при тех же вводных - предлагайте😉
#async
The White House's climate policy may lead to 1.3 million deaths 🚨🔥, an analysis by ProPublica and the Guardian shows that Donald Trump's decisions, which return the country's oil, gas and coal production to previous volumes and lift climate restrictions, will increase emissions and lead to 1.3 million additional deaths from heat in the future 😱⛏️, writes the Guardian.
The poor and hot countries of Africa and South Asia will bear the brunt. 🌍🔥 Scientists are already seeing an increase in mortality. In the United States, the number of deaths due to heat has increased by more than 50% since 2000. ☀️📈 In India and Pakistan, heat above 48 degrees is already causing deaths in towns and villages. 🔥🏞 The elderly, children, and those who work on the streets suffer more than others. 👵👶🏃♂️ The authors of the analysis believe that the increase in emissions in the next ten years will worsen the situation. 🗣💨
Researchers say that the United States accounts for a fifth of the world's emissions, but it will see only a small increase in deaths. 🇺🇸🌍 The regions where people already live in extreme heat will suffer the most.
#Trump#ClimateChanges
👂More on Trump's Ear
The Trump administration announced it will pay nearly $1 billion to French energy giant TotalEnergies in exchange for the company abandoning plans to build offshore wind farms in the Atlantic Ocean and instead pursue fossil fuel projects in the US. ⚡️
The current administration has thrown up roadblocks at every turn for offshore wind projects; a type of energy that President Donald Trump has personally reviled for years. After trying and failing to block construction on more mature projects, this announcement is the first sign of a new strategy: The federal government is paying to stop wind farms before they begin. 🌬
Last year, the Trump Interior Department took the step of stopping the approval of federal permits for renewable energy projects, a move that effectively killed offshore wind projects in early development. Monday’s deal builds on that, by trying to ensure companies can’t continue building under a future administration friendlier to offshore wind. 🏛
The government is paying back TotalEnergies for federal leases it purchased under the Biden administration to develop two offshore wind farms off the coasts of New York and North Carolina. The Justice Department will use nearly $1 billion in taxpayer funds to reimburse the company for money it spent to purchase leases under the Biden administration. 💰
Together, those two projects could have generated more than 4 gigawatts of electricity for US households and businesses, according to developers. ⚡️
#Energy#ClimateChanges
👂More on Trump's Ear ⚠️
Trump canceled a key point of the US climate policy, calling greenhouse gases harmless. US President Donald Trump has achieved the cancellation of the decision on which the American climate policy has been based since 2009.
The U.S. Environmental Protection Agency (EPA) has invalidated the Obama-era conclusion that carbon dioxide and other greenhouse gases pose a threat to public health and well-being. It was on this norm that the key climate restrictions under the Clean Air Act were based, from emission limits for cars to requirements for power plants and industry. Trump and EPA chief Lee Zeldin called the move "the largest deregulation in American history." According to Trump, the previous rules were "disastrous," hurting the auto industry and "inflating prices for consumers."
Environmentalists call the cancellation a historic setback and the biggest blow to the United States' ability to combat climate change.
#Trump#Ecology#ClimateChanges
👂More on Trump's Ear ⚠️