semvec/pypi-packages huggingface dataset:
colorblind-async 0.0.1
Allows call await from synchronous context
Requires Python: >=3.7

# colorblind-async


> **Warning**
> This is only a proof-of-concept.


## Usage

1. import `ColorblindAsync` from `colorblind_async`
2. init by: `ColorblindAsync.init()`
3. include files/modules with awaits outside async context
4. run

### Example

main.py
```python
from colorblind_async import ColorblindAsync

ColorblindAsync.init()

import file # now when you include other modules, the colorblind_async will do it's job

# await my_coro() # you cannot do that here, colorblind_async binds to import statement and it won't work here
```

file.py
```python
async def my_coro():
    return "foo"

def my_sync_fn():
    await my_coro() #  call `await` in sync function

await my_coro() # I can even call `await` outside any function
```


## Install

```sh
pip3 install colorblind-async
```

## Optional setup

### Pass already defined event loop

```python
from colorblind_async import ColorblindAsync

ColorblindAsync.init(loop=my_loop)

```

## Debug

Prints altered code with `await`s outside async context replaced with `__colorblind_async__(coro)` call.

`DEBUG_COLORBLIND_ASYNC=True python script.py`

## Run Tests

`make test`

## How does it work

After you import `colorblind_async` module, it'll enhance the `include` function to catch `SyntaxError` `await outside function...`. When it catches it, it parse AST of the included file and replace `await` keyword with call `__colorblind_async__(coroutine)`. `__colorblind_async__` is simple wrapper which executes passed coroutine in the (provided) event loop.
Because you can call sync functions from async context and then call async context in a nested fashion, library `nest-asyncio` is used for reentrant async semantics.

## Limitations

As per library `nest-asyncio`: _only event loops from asyncio can be patched; Loops from other projects, such as uvloop or quamash, generally can't be patched._

Be aware of unexpected preemptions as they may occur outside of visible `async` keyword context and you may need to apply additional synchronization.

## FAQ

### Why?

Python async is more complicated than it needs to be. And I like the idea of [Zig's colorblind Async/Await](https://kristoff.it/blog/zig-colorblind-async-await/). Obviously there are cases when you do not care about event loop stuff and just what the code running without spending too much time on dealing with sync/async parts calling each other.

### Is this dark magic?

Well, um, technically yes. Because it allows you to import _syntactically incorrect_ program, so use it with caution. But don't worry, no kittens were harmed during the experiments.
trality-blog-zh 2022.12.14.0
Trality 博客中文翻译
Requires Python: >=3.6

# Trality 博客中文翻译

> 原文：[Trality Blog](https://www.trality.com/blog)
> 
> 协议：[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
> 
> 阶段：机翻（1）
> 
> 勇敢的人你可愿，为这份信念肩并肩？在这街垒的那一边，光明的未来可望见。你可听到这歌声？听鼓声在远方回响。未来与新希望同在，曙光在前方。——《悲惨世界》终曲
> 
> 列に入れよ、我らの味方に。砦の向こうに、憧れの世界。皆聞こえるか？ドラムの響が。彼ら夢見た明日が来るよ。
> 
> Will you joinin our crusade? Who will be strong and stand with me? Somewhere beyond the barricade, is there a world you long to see? Do you hear the people sing? Say, do youhear the distant drums? It is the future that they bring when tomorrow comes!

* [在线阅读](https://trality.apachecn.org)
* [在线阅读（Gitee）](https://apachecn.gitee.io/doc-template/)
* [ApacheCN 学习资源](http://docs.apachecn.org/)
* [ApacheCN 翻译校对兼职群 713436582](https://jq.qq.com/?_wv=1027&k=VSNtgpjb)

## 贡献指南

为了不断改进翻译质量，我们特此启动了【翻译、校对、笔记整理活动】，开设了多个校对项目。贡献者校对一章之后可以领取千字2\~4元的奖励。进行中的校对活动请见[活动列表](https://home.apachecn.org/#/docs/activity/docs-activity)。更多详情请联系飞龙（Q562826179，V:wizardforcel）。

## 联系方式

### 负责人

* [飞龙](https://github.com/wizardforcel): 562826179

### 其他

*   在我们的 [apachecn/trality-blog-zh](https://github.com/apachecn/trality-blog-zh) github 上提 issue.
*   发邮件到 Email: `apachecn@163.com`.
*   在我们的 [组织学习交流群](https://www.apachecn.org/#/docs/join) 中联系群主/管理员即可.

## 下载

### Docker

```
docker pull apachecn0/trality-blog-zh
docker run -tid -p <port>:80 apachecn0/trality-blog-zh
# 访问 http://localhost:{port} 查看文档
```

### PYPI

```
pip install trality-blog-zh
trality-blog-zh <port>
# 访问 http://localhost:{port} 查看文档
```

### NPM

```
npm install -g trality-blog-zh
trality-blog-zh <port>
# 访问 http://localhost:{port} 查看文档
```

## 其它协议

霞鹜文楷采用 [SIL 开放字体协议 1.1](https://github.com/lxgw/LxgwWenKai/blob/main/SIL_Open_Font_License_1.1.txt)。

## 赞助我们

![](http://data.apachecn.org/img/about/donate.jpg)