1import asyncio 2import decimal 3import unittest 4 5 6class DecimalContextTest(unittest.TestCase): 7 8 def test_asyncio_task_decimal_context(self): 9 async def fractions(t, precision, x, y): 10 with decimal.localcontext() as ctx: 11 ctx.prec = precision 12 a = decimal.Decimal(x) / decimal.Decimal(y) 13 await asyncio.sleep(t) 14 b = decimal.Decimal(x) / decimal.Decimal(y ** 2) 15 return a, b 16 17 async def main(): 18 r1, r2 = await asyncio.gather( 19 fractions(0.1, 3, 1, 3), fractions(0.2, 6, 1, 3)) 20 21 return r1, r2 22 23 r1, r2 = asyncio.run(main()) 24 25 self.assertEqual(str(r1[0]), '0.333') 26 self.assertEqual(str(r1[1]), '0.111') 27 28 self.assertEqual(str(r2[0]), '0.333333') 29 self.assertEqual(str(r2[1]), '0.111111') 30