Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Probable bug with scalene when profiling async code #805

Open
dheerajck opened this issue Apr 7, 2024 · 1 comment
Open

Probable bug with scalene when profiling async code #805

dheerajck opened this issue Apr 7, 2024 · 1 comment

Comments

@dheerajck
Copy link

dheerajck commented Apr 7, 2024

Python 3.11.6
Scalene version 1.5.38 (2024.03.24)

Profiled values arent in the correct place

To reproduce run this
scalene --cpu-percent-threshold 0 async_await.py --cli
and press Control + C after 3 seconds from when program prints "Task 1 done"

#  async_await.py
import asyncio


async def task():
    print('Task 1')
    await asyncio.sleep(5)
    a = 1
    a = a * 100
    print('Task 1 done')


async def gather():
    print('Gather 1')
    asyncio.create_task(task())
    print('Gather 1 done')


async def main():
    await asyncio.gather(gather())
    while True:
        await asyncio.sleep(1)

    print('Main done')


if __name__ == '__main__':
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        pass

output

/home/dheeraj/playground/profiler_test/async_await.py: % of time = 100.00% (5.009s) out of 5.009s.                                                       
       ╷       ╷       ╷       ╷        ╷       ╷               ╷       ╷                                                                                                                                       
       │Time   │–––––– │–––––– │Memory  │–––––– │–––––––––––    │Copy   │                                                                                                                                       
  Line │Python │native │system │Python  │peak   │timeline/%     │(MB/s) │/home/dheeraj/playground/profiler_test/async_await.py                                                                                  
╺━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━┿━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━┿━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸
     1 │       │       │       │        │       │               │       │import asyncio                                                                                                                         
     2 │       │       │       │        │       │               │       │                                                                                                                                       
     3 │       │       │       │        │       │               │       │                                                                                                                                       
     4 │       │       │       │        │       │               │       │async def task():                                                                                                                      
     5 │       │       │       │        │       │               │       │    print('Task 1')                                                                                                                    
     6 │       │       │  [1]  │        │       │               │       │    await asyncio.sleep(5)                                                                                                             
     7 │       │       │       │        │       │               │       │    a = 1                                                                                                                              
     8 │  [2]  |       |       │        │       │               │       │    a = a + 1                                                                                                                          
     9 │       │       │       │        │       │               │       │    print('Task 1 done')                                                                                                               
    10 │       │       │       │        │       │               │       │    exit(0)                                                                                                                            
    11 │       │       │       │        │       │               │       │                                                                                                                                       
    12 │       │       │       │        │       │               │       │                                                                                                                                       
    13 │       │       │       │        │       │               │       │async def gather():                                                                                                                    
    14 │       │       │       │        │       │               │       │    print('Gather 1')                                                                                                                  
    15 │       │       │       │        │       │               │       │    asyncio.create_task(task())                                                                                                        
    16 │       │       │       │        │       │               │       │    print('Gather 1 done')                                                                                                             
    17 │       │       │       │        │       │               │       │                                                                                                                                       
    18 │       │       │       │        │       │               │       │                                                                                                                                       
    19 │       │       │       │        │       │               │       │async def main():                                                                                                                      
    20 │       │       │       │        │       │               │       │    await asyncio.gather(gather())                                                                                                     
    21 │       │       │       │        │       │               │       │    while True:                                                                                                                        
    22 │       │       │       │        │       │               │       │        await asyncio.sleep(1)                                                                                                         
    23 │       │       │       │        │       │               │       │                                                                                                                                       
    24 │       │       │       │        │       │               │       │    print('Main done')                                                                                                                 
    25 │       │       │       │        │       │               │       │                                                                                                                                       
    26 │       │       │       │        │       │               │       │                                                                                                                                       
    27 │       │       │       │        │       │               │       │if __name__ == '__main__':                                                                                                             
    28 │    1% │       │  99%  │        │       │               │       │    asyncio.run(main())    
    ```

I would expect some numbers at [1] and [2]
is this the intended behaviour when profiling async code with async tasks or is it a bug
@dheerajck
Copy link
Author

profiler_test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant