我正在尝试react-router v6,我觉得在涉及路由器时我错过了一个重要的概念loader。例如,在react-router 教程中讨论了loader路由的/contacts/:contactId,我发现您可以轻松访问 URL 中包含的任何状态,就像contactId本例中的 。然而,我发现除了路由参数之外,我还需要像 a 这样的东西userId,通常我可以通过 prop 或上下文提供者获得这些东西,因为我喜欢使身份验证信息更加全局化。所以我很难了解如何访问loader函数内部的全局状态。
更具体地说,我的index.js文件是这样设置的,我在其中初始化router对象,然后将其传递给root.render()函数:
const router = createBrowserRouter([
{
path: "/",
element: <App />,
children: [
{
path: "/user-specific-resources",
element: <UserResources />,
loader: resourceLoader,
},
}
]);
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
Run Code Online (Sandbox Code Playgroud)
但是,resourceLoader路由函数/user-specific-resources需要知道要传递给服务器的 userId/token。我知道我可以将 userId 作为路由参数传递,例如/user-specific-resources/:userId,但这感觉有点尴尬,因为它userId …
因此asyncio,既然 3.8 已发布,我将再次尝试该模块。但是,在尝试正常关闭事件循环时,我得到了意想不到的结果。具体来说,我正在侦听 a SIGINT,取消正在运行的Tasks,收集这些Tasks,然后.stop()执行事件循环。我知道当它们被取消时Tasks 会引发一个CancelledError,这将传播并结束我的调用,asyncio.gather除非根据文档,我传递return_exceptions=True给asyncio.gather,这应该导致gather等待所有Tasks 取消并返回一个CancelledErrors数组。但是,如果我尝试取消s ,似乎return_exceptions=True仍然会立即中断我的gather通话。gatherTask
这是重现效果的代码。我正在运行 python 3.8.0:
# demo.py
import asyncio
import random
import signal
async def worker():
sleep_time = random.random() * 3
await asyncio.sleep(sleep_time)
print(f"Slept for {sleep_time} seconds")
async def dispatcher(queue):
while True:
await queue.get()
asyncio.create_task(worker())
tasks = asyncio.all_tasks()
print(f"Running Tasks: {len(tasks)}") …Run Code Online (Sandbox Code Playgroud) 我正在尝试write使用 Rust 中的内联汇编来执行系统调用。基本上我正在尝试重现这个 hello world 示例。我试图传递对字符串切片的引用作为我的消息,但我收到以下错误,该错误似乎与其自身相矛盾:
cannot use value of type `*const str` for inline assembly
only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
Run Code Online (Sandbox Code Playgroud)
代码如下,但我的问题是:
*const str?cannot use value of type `*const str` for inline assembly
only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
Run Code Online (Sandbox Code Playgroud)
它在 MacOS v13.6、M1 芯片 (Armv8.5-a) 上运行。