Я использую это с langgraph для управления потоком.
Код: Выделить всё
await page.mouse.click(x, y)
await page.keyboard.press()
await page.* # Almost all page functions in playwright
Код: Выделить всё
await asyncio.wait_for(page.mouse.click(x, y), timeout=1.0)
await asyncio.wait_for(page.keyboard.type(text), timeout=1.0)
Я не могу точно понять, в чем проблема, и у меня есть какое-то время застрял на этом.
Так я называю драматургические объекты. Операции драматурга начинают зависать внутри функции __run() и до этого прекрасно работают с простым ожиданием.
Код: Выделить всё
async def run(self, task: str):
async with async_playwright() as playwright:
# Setup the browser object for the run
self.browser = await playwright.firefox.launch(headless=False)
self.page = await self.browser.new_page()
await self.page.goto("https://www.google.com")
await self.__run(task)
Код: Выделить всё
async def __run(self, task: str):
inputs = {
"task": [
HumanMessage(
content=task
)
],
"page": self.page
}
async for output in self.graph.with_config({"run_name": "Test Run"}).astream(inputs):
# stream() yields dictionaries with output keyed by node name
for key, value in output.items():
print(f"Output from node '{key}':")
print("---")
# print(value)
print("\n---\n")
Код: Выделить всё
def execution_node(state: AgentState) -> AgentState:
# ... response and state constructed for passing below
state = asyncio.run(process_tools(response, state))
logger.debug("Finished processing tools")
return state
async def process_tools(response, state):
# ... Truncated irrelevant code
action = ToolInvocation(
tool=tool_call["function"]["name"],
tool_input=args,
id=tool_call["id"],
)
# We call the tool_executor and get back a response
tool_response = await tool_executor.invoke(action)
# Example tool invocation function
async def click(state: AgentState, bbox_id: int):
x, y = X1, X2
await page.mouse.click(x, y) # ISSUE: Not exiting
# await asyncio.wait_for(page.mouse.click(x, y), timeout=1.0) # This approach works fine
Я подтвердил, что выполнение зависает только для методов страницы драматурга (при использовании простого ожидания / обеспечения_будущего / create_task), а не для других функций.
Подробнее здесь: https://stackoverflow.com/questions/791 ... less-force