Comments (4)
The behavior of coro::sleep
depends on the behavior of executors. Concretely, it depends on the implementation of Executor::after(std::chrono::duration<T,U>)
. And it is the general idea of async_simple to make the behavior configurable with the executors.
from async_simple.
SimpleExecutor is just an Executor for testing. It is not part of libasync_simple. You need to define your own Executor.
from async_simple.
We use SimpleExecutor + brpc TimerThread for production currently. This is an example of custom coroutine sleep.
class BGExecutor : public async_simple::executors::SimpleExecutor {
public:
static StatusOr<std::unique_ptr<BGExecutor>> NewBGExecutor(
size_t thread_num) noexcept {
auto ptr = std::make_unique<BGExecutor>(thread_num);
auto s = ptr->Init();
if (!s.ok()) {
return s;
}
return ptr;
}
~BGExecutor() override = default;
static void TimerTaskFn(void* arg) {
auto fptr = std::unique_ptr<std::function<void()>>(
static_cast<std::function<void()>*>(arg));
(*fptr)();
}
BGExecutor(size_t thread_num)
: async_simple::executors::SimpleExecutor(thread_num) {}
private:
Status Init() {
bthread::TimerThreadOptions options;
const int rc = timer_.start(&options);
// rc != 0 when pthread create failed
// CHECK_IF_RET(rc == 0, Status::Err(),
// "Init BGExecutor. start timer failed : {}. ", rc);
return Status::OK();
}
void schedule(Func func, Duration dur) override {
async_simple::Executor::Context ctx = checkout();
auto fptr = std::make_unique<std::function<void()>>(
[this, ctx, func = std::move(func)]() {
checkin(func, ctx, async_simple::ScheduleOptions{});
});
const auto sec = std::chrono::duration_cast<std::chrono::seconds>(dur);
auto ts = timespec{
.tv_sec = sec.count(),
.tv_nsec =
std::chrono::duration_cast<std::chrono::nanoseconds>(dur - sec)
.count()};
auto* args = fptr.release();
auto task_id = timer_.schedule(TimerTaskFn, args, ts);
if (unlikely(task_id == bthread::TimerThread::INVALID_TASK_ID)) {
// BG3_ERROR("schedule timer task failed");
TimerTaskFn(args);
}
}
bthread::TimerThread timer_;
};
from async_simple.
Closed due to no more questions. Feel free to require to reopen this.
from async_simple.
Related Issues (20)
- We need functionality similar to asio::dispatch HOT 1
- xmake build failed on windows HOT 4
- Deploy static content to Pages task is getting failure HOT 3
- a bug in specifying executors for Future HOT 11
- What about add a new feature lazy_local HOT 6
- Perhaps more information can be exposed to the Executor to guide scheduling HOT 13
- Try.h报错信息拼写错误
- how to resume a Yield coro HOT 2
- 可否提供一个客户端异步http请求的例子? HOT 11
- ConditionVariable::notifyOne 可能miss唤醒,
- 請問如何實現類似epoll的模型? HOT 4
- There might be better to use like `asyncio.gather` in example of `StacklessCoroutineAndFuture.md` HOT 1
- coro::ConditionVariable is not aware of Executor HOT 3
- A question about Mutex implementation HOT 3
- GTEST_INCLUDE_DIR-NOTFOUND HOT 2
- how to adapt to callback style API HOT 3
- Bug Report: Extra Increment on Generator::iterator After Completion Causes Crash Using Clang15 HOT 6
- What are the best practices for cancellation? HOT 1
- 相比于老式的异步编程, switch延时如何? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async_simple.