2 Star 3 Fork 2

james / asyncio

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

What is AsyncIO?

AsyncIO is a C++ coroutine helper library based on llvm-5.0's (or above) coroutine feature. AsycnIO v0.2 has two components:

  • Couroutine Coroutine makes it much easier to write coroutine methods, generators and asynchronous generators. These part of AsyncIO is header only that means if this is all you need, you can just download the source code into your projects, then use it without build AsyncIO
  • EventLoop EventLoop is a simple scheduler based on libuv, inspired by EventLoop of Python3. it helps you run multiple coroutines "simultaneously" within a thread.

How to Install

install dependency

Make sure you have installed llvm 5.0 or above, libc++, libc++abi, libuv and cmake.

On MacOS X
brew tap homebrew/versions
brew install --HEAD llvm #this will install libc++ by default
brew install cmake
brew install 
On Ubuntu
sudo apt-get install libuv cmake
# install llvm-5.0 libc++ libc++abi to /usr/local
# download from here http://releases.llvm.org/download.html#5.0.0

Build and Install

cd $ASYNCIO_PATH && mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j 10
make test
make install

Examples

There are some examples in asyncio/examples directory. For more detail infomation about specific class, you can check the tests in asyncio/tests directory or just have a look at the code.

sleep_sort.cpp

Source Code: sleep_sort.cpp

#include <asyncio/asyncio.hpp>
#include <iostream>

using namespace std;
using namespace asyncio;

coro<void> sleepCout(EventLoop *loop, uint64_t value) {
  co_await sleep(loop, value);
  cout << value << endl;
}

int main() {
  EventLoop loop;
  cout << "Someone says 'sleep sort' is O(1)?" << endl;
  uint64_t max = -1;
  for (auto &&v : {5, 1, 9, 7, 3}) {
    loop.createTask(sleepCout(&loop, v))->release();
    max = max > v ? max : v;
  }
  loop.callLater(max + 20, [&] { loop.stop(); })->release();
  loop.runForever();
  cout << "... I think it makes sense! 🤣🤣🤣" << endl;
}

compile it

$ clang -I $ASYNCIO_HEADER_PATH -o sleep_sort -lc++ -std=c++14 -stdlib=libc++ -fcoroutines-ts -lasyncio -L $ASYNCIO_LIB_PATH -rpath $ASYNCIO_LIB_PATH sleep_sort.cpp
 
$ ./sleep_sort
Someone says 'sleep sort' is O(1)?
1
3
5
7
9
... I think it makes sense! 🤣🤣🤣
MIT License Copyright (c) 2017 zhangli Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A c++ coroutine library making use of llvm 5.0 coroutine experimental features to dramatically simplify asynchronous logical in C++. 展开 收起
C++
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C++
1
https://gitee.com/zhanglix/asyncio.git
git@gitee.com:zhanglix/asyncio.git
zhanglix
asyncio
asyncio
master

搜索帮助