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

Async feature implementation using threading library #28

Merged
merged 14 commits into from
Jul 28, 2023

Conversation

NimaSamadi007
Copy link
Contributor

Due to the previous inconsistencies of async feature implementation, the threading library is used to implement the async-like sound-playing feature. Tests are provided in the test directory. Github build fails due to some issues with the function_test.py script which I'm not sure why.
It seems that when the parent thread of the subprocess is dead, its child process doesn't die automatically. In other words, in async mode, if the user program ends before the sound playing is finished, the user can hear the voice although the program is terminated. Therefore, it seems that we need some sort of cleanup procedure to kill/terminate these child threads so they don't end up being zombie processes. The best way to do this is to use class destructors but as of now, the code is more function-oriented rather than object-oriented. For now, I used Python's atexit library to run the cleanup procedure. Any other comments are welcome!

Fix issues in decorators
* Async bugs: call the play function async from test script
* Test script: more intuitive example of test script
* Async feature: Now user will get a handling thread which
then could be used for waiting for the sound playing to be ended
or do other tasks in the meantime.
* Update documentations
@sadrasabouri sadrasabouri mentioned this pull request Jul 17, 2023
@codecov
Copy link

codecov bot commented Jul 17, 2023

Codecov Report

Patch coverage: 42.42% and project coverage change: -30.87% ⚠️

Comparison is base (3d3ec51) 100.00% compared to head (fb5b259) 69.13%.

Additional details and impacted files
@@             Coverage Diff              @@
##               dev      #28       +/-   ##
============================================
- Coverage   100.00%   69.13%   -30.87%     
============================================
  Files            3        3               
  Lines           56       81       +25     
  Branches         4        7        +3     
============================================
  Hits            56       56               
- Misses           0       24       +24     
- Partials         0        1        +1     
Files Changed Coverage Δ
nava/functions.py 65.27% <42.42%> (-34.73%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@sepandhaghighi sepandhaghighi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NimaSamadi007 Well done 💯
This approach looks good to me!

  1. I think it would be better to return proc in the __play_sync_linux function instead of declaring a new function like __play_non_blocking_linux. This work will prevent future duplications.

  2. It seems there is a minor issue in import/call of the __cleanup_processes function in __init__.py

  3. Continue your work for other platforms

Good luck 🔥

@sepandhaghighi sepandhaghighi marked this pull request as draft July 17, 2023 22:25
@NimaSamadi007
Copy link
Contributor Author

Thanks 🙏, that's great.

  • Yeah sure. I will call __play_sync_linux in the thread target function. However, it seems that there's no straightforward way to access the return value of the thread. There are two approaches:
    1. Use a global variable or an input variable that acts like the return value (similar to what's done in C)
    2. Modify run() method in class definition
      I will use the first approach and always save the process handler in a list.
  • I will test it again. Thanks for mentioning.
  • Yeah sure. I will also develop the code for Windows, but I'm not sure how I'm supposed to test the code for MacOS.

…- Finish Linux implementation

* __non_blocking_play() is removed
* cleanup_processes function name changed to make sure it's imported
* Linux implementation completed and tested
@NimaSamadi007
Copy link
Contributor Author

Hello all. I implemented the Windows async feature and applied the requested codebase changes. Please help to check it. I don't have access to a MacOS-based device. So, I couldn't implement the async feature for MacOS. Should I install it in a virtualized environment and work on it or do you have any other suggestions? I haven't worked with MacOS before so I don't know if it's even possible to do this.

@sadrasabouri
Copy link
Member

Hello all. I implemented the Windows async feature and applied the requested codebase changes. Please help to check it. I don't have access to a MacOS-based device. So, I couldn't implement the async feature for MacOS. Should I install it in a virtualized environment and work on it or do you have any other suggestions? I haven't worked with MacOS before so I don't know if it's even possible to do this.

Thanks Nima. You just needed to implement macOS by replacing aplay by afplay. I will test it in my friend's laptop.

@NimaSamadi007
Copy link
Contributor Author

I implemented the macOS async feature similar to Linux implementation. By the way, in Windows, you can specify a flag to play the sound in async mode and you don't need to worry about the underlying process getting killed or not and everything is handled in winsound. However, this causes some sort of inconsistent API across different operating systems. In other words, in Windows, high-level users won't get any thread handler while that's not the case in Linux and macOS. I'm not sure if it's okay or not.

Copy link
Member

@sadrasabouri sadrasabouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Nima. It looks good to me.
Just remove test/__init__.py and test/main.py. We will add tests for it after this PR.

@sepandhaghighi sepandhaghighi marked this pull request as ready for review July 25, 2023 12:55
@sepandhaghighi sepandhaghighi added enhancement New feature or request new feature labels Jul 25, 2023
@sepandhaghighi sepandhaghighi added this to the nava v0.3 milestone Jul 25, 2023
Copy link
Member

@sepandhaghighi sepandhaghighi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@sepandhaghighi sepandhaghighi merged commit 78992c6 into openscilab:dev Jul 28, 2023
12 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants