小编Zak*_*cke的帖子

asyncio: running task only if all other tasks are awaiting

I am currently running some endless tasks using asyncio.wait

I need a special function to run when all the others are on await

import asyncio 

async def special_function():
    while True:
        # does some work, 
        # Passes control back to controller to run main_tasks
        # if they are no longer waiting.
        await asyncio.sleep(0)

async def handler():
    tasks = [task() for task in main_tasks]

    # Adding the task that I want to run when all main_tasks are awaiting:
    tasks.append(special_function())

    await asyncio.wait(tasks)

asyncio.get_event_loop().run_until_complete(handler()) …
Run Code Online (Sandbox Code Playgroud)

python concurrency python-asyncio

10
推荐指数
1
解决办法
559
查看次数

确定浮动的实际精度

我正在与返回浮点数的API进行交互.我试图计算API创建这些浮点数的小数位数.

例如:

# API returns the following floats.
>> 0.0194360600000000015297185740.....
>> 0.0193793800000000016048318230.....
>> 0.0193793699999999999294963970.....

# Quite clearly these are supposed to represent:
>> 0.01943606
>> 0.01937938
>> 0.01937937

# And are therefore ACTUALLY accurate to only 8 decimal places. 
Run Code Online (Sandbox Code Playgroud)

如何确定浮点数实际上精确到小数点后8位?一旦我这样做,我可以decimal.Decimal使用"true"值而不是不准确的浮点数初始化一个实例.

编辑:API返回的准确小数位数会有所不同,并不总是8!

python precision decimal floating-accuracy

2
推荐指数
1
解决办法
62
查看次数