Skip to main content

\Co\sleep

⚠️ This page was initialized by AI translation and may contain outdated or inaccurate information. If there are inaccuracies, please submit changes to correct these errors Correct

###API

namespace Co;

function sleep(int $second): void;

Parameter Description​

ParametersTypeDescription
$secondintSleep time, unit second, supports decimal precision of 0.1 seconds

return value​

No return value

Overview​

Sleep (sleep), the user suspends the execution of the current coroutine and lets the CPU handle other to-do tasks.

  • Using the \Co\sleep function in fiber space: will suspend the current coroutine, giving up CPU resources, and other coroutines can continue to execute.
  • Using the \Co\sleep function outside the fiber space: will suspend the current main process, giving up CPU resources, and other coroutines can continue to execute.

\Co\sleep is different from \sleep in that it acts on the current coroutine, not the entire process.

Basic usage​

\Co\async(function () {
\Co\sleep(1);

echo 'async task';
});

\Co\sleep(10); // Suspend the main process for 10 seconds so that other coroutines can complete the task

Precautions​

None