MCPcopy Index your code
hub / github.com/RustPython/RustPython / _swap_current_task

Function _swap_current_task

crates/stdlib/src/_asyncio.rs:2530–2556  ·  view source on GitHub ↗
(loop_: PyObjectRef, task: PyObjectRef, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

2528
2529 #[pyfunction]
2530 fn _swap_current_task(loop_: PyObjectRef, task: PyObjectRef, vm: &VirtualMachine) -> PyResult {
2531 // Per-thread swap, matching CPython's swap_current_task
2532 let prev = vm
2533 .asyncio_running_task
2534 .borrow()
2535 .clone()
2536 .unwrap_or_else(|| vm.ctx.none());
2537
2538 if vm.is_none(&task) {
2539 *vm.asyncio_running_task.borrow_mut() = None;
2540 } else {
2541 *vm.asyncio_running_task.borrow_mut() = Some(task.clone());
2542 }
2543
2544 // Also update the module-level dict for cross-thread queries
2545 if let Ok(current_tasks) = get_current_tasks_dict(vm)
2546 && let Ok(dict) = current_tasks.downcast::<rustpython_vm::builtins::PyDict>()
2547 {
2548 if vm.is_none(&task) {
2549 let _ = dict.del_item(&*loop_, vm);
2550 } else {
2551 let _ = dict.set_item(&*loop_, task, vm);
2552 }
2553 }
2554
2555 Ok(prev)
2556 }
2557
2558 /// Reset task state after fork in child process.
2559 #[pyfunction]

Callers 1

task_eager_startFunction · 0.70

Calls 9

get_current_tasks_dictFunction · 0.85
noneMethod · 0.80
borrow_mutMethod · 0.80
SomeClass · 0.50
cloneMethod · 0.45
borrowMethod · 0.45
is_noneMethod · 0.45
del_itemMethod · 0.45
set_itemMethod · 0.45

Tested by

no test coverage detected