ref: Remove some useless asyncio.to_thread (#5149)

Remove some useless asyncio.to_thread
This commit is contained in:
Christophe Bornet 2024-12-08 20:13:08 +01:00 committed by GitHub
commit c0b25fa651
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 44 additions and 94 deletions

View file

@ -46,20 +46,28 @@ load_dotenv()
@pytest.fixture(autouse=True)
def blockbuster():
with blockbuster_ctx() as bb:
for func in [
"io.BufferedReader.read",
"io.BufferedWriter.write",
"io.TextIOWrapper.read",
"io.TextIOWrapper.write",
]:
bb.functions[func].can_block_functions.append(("settings/service.py", {"initialize"}))
for func in bb.functions:
if func.startswith("sqlite3."):
bb.functions[func].deactivate()
bb.functions["threading.Lock.acquire"].deactivate()
yield bb
def blockbuster(request):
if "benchmark" in request.keywords:
yield
else:
with blockbuster_ctx() as bb:
for func in [
"io.BufferedReader.read",
"io.BufferedWriter.write",
"io.TextIOWrapper.read",
"io.TextIOWrapper.write",
]:
bb.functions[func].can_block_functions.append(("settings/service.py", {"initialize"}))
for func in [
"io.BufferedReader.read",
"io.TextIOWrapper.read",
]:
bb.functions[func].can_block_functions.append(("importlib_metadata/__init__.py", {"metadata"}))
for func in bb.functions:
if func.startswith("sqlite3."):
bb.functions[func].deactivate()
bb.functions["threading.Lock.acquire"].deactivate()
yield bb
def pytest_configure(config):

View file

@ -1,18 +1,11 @@
import asyncio
from pathlib import Path
from typing import Any
from aiofile import async_open
from fastapi import status
from httpx import AsyncClient
from langflow.api.v1.schemas import UpdateCustomComponentRequest
async def get_dynamic_output_component_code():
return await asyncio.to_thread(
Path("src/backend/tests/data/dynamic_output_component.py").read_text, encoding="utf-8"
)
async def test_get_version(client: AsyncClient):
response = await client.get("api/v1/version")
result = response.json()
@ -37,7 +30,8 @@ async def test_get_config(client: AsyncClient):
async def test_update_component_outputs(client: AsyncClient, logged_in_headers: dict):
code = await get_dynamic_output_component_code()
async with async_open("src/backend/tests/data/dynamic_output_component.py", encoding="utf-8") as f:
code = await f.read()
frontend_node: dict[str, Any] = {"outputs": []}
request = UpdateCustomComponentRequest(
code=code,

View file

@ -1,6 +1,5 @@
import asyncio
import pytest
from aiofile import async_open
from langflow.components.inputs import ChatInput, TextInputComponent
from langflow.schema.message import Message
from langflow.utils.constants import MESSAGE_SENDER_AI, MESSAGE_SENDER_NAME_USER, MESSAGE_SENDER_USER
@ -93,7 +92,8 @@ class TestChatInput(ComponentTestBaseWithClient):
"""Test message response with file attachments."""
# Create a temporary test file
test_file = tmp_path / "test.txt"
await asyncio.to_thread(test_file.write_text, "Test content")
async with async_open(test_file, "w") as f:
await f.write("Test content")
kwargs = {
"input_value": "Message with file",

View file

@ -1,4 +1,3 @@
import asyncio
import logging
from collections import deque
@ -14,7 +13,7 @@ from langflow.graph.graph.constants import Finish
async def test_graph_not_prepared():
chat_input = ChatInput()
chat_output = ChatOutput()
graph = await asyncio.to_thread(Graph)
graph = Graph()
graph.add_component(chat_input)
graph.add_component(chat_output)
with pytest.raises(ValueError, match="Graph not prepared"):
@ -24,7 +23,7 @@ async def test_graph_not_prepared():
async def test_graph(caplog: pytest.LogCaptureFixture):
chat_input = ChatInput()
chat_output = ChatOutput()
graph = await asyncio.to_thread(Graph)
graph = Graph()
graph.add_component(chat_input)
graph.add_component(chat_output)
caplog.clear()
@ -36,7 +35,7 @@ async def test_graph(caplog: pytest.LogCaptureFixture):
async def test_graph_with_edge():
chat_input = ChatInput()
chat_output = ChatOutput()
graph = await asyncio.to_thread(Graph)
graph = Graph()
input_id = graph.add_component(chat_input)
output_id = graph.add_component(chat_output)
graph.add_component_edge(input_id, (chat_input.outputs[0].name, chat_input.inputs[0].name), output_id)
@ -58,7 +57,7 @@ async def test_graph_functional():
chat_input.set(should_store_message=False)
chat_output = ChatOutput(input_value="test", _id="chat_output")
chat_output.set(sender_name=chat_input.message_response)
graph = await asyncio.to_thread(Graph, chat_input, chat_output)
graph = Graph(chat_input, chat_output)
assert graph._run_queue == deque(["chat_input"])
await graph.astep()
assert graph._run_queue == deque(["chat_output"])
@ -73,7 +72,7 @@ async def test_graph_functional_async_start():
chat_input = ChatInput(_id="chat_input")
chat_output = ChatOutput(input_value="test", _id="chat_output")
chat_output.set(sender_name=chat_input.message_response)
graph = await asyncio.to_thread(Graph, chat_input, chat_output)
graph = Graph(chat_input, chat_output)
# Now iterate through the graph
# and check that the graph is running
# correctly

View file

@ -1,5 +1,3 @@
import asyncio
from langflow.processing.process import process_tweaks
from langflow.services.deps import get_session_service
@ -265,7 +263,7 @@ def test_tweak_not_in_template():
async def test_load_langchain_object_with_cached_session(basic_graph_data):
# Provide a non-existent session_id
session_service = await asyncio.to_thread(get_session_service)
session_service = get_session_service()
session_id1 = "non-existent-session-id"
graph1, artifacts1 = await session_service.load_session(session_id1, basic_graph_data)
# Use the new session_id to get the langchain_object again