I want to record voices using vosk in the background, while in the main thread I still read my ultrasonic sensors values without stopping the script, but when I try to do so, threads don't run asynchronously
-
Please provide enough code so others can better understand or reproduce the problem. – Community Oct 21 '22 at 17:03
1 Answers
Python has excellent support for threading and coroutines, and this is perfect for I/O intensive tasks. A task can be waiting for a web response, for example, and another making some other requests or querying databases.
For CPU-intensive work, you could think that multithreading is a fine tool to take advantage of multicore processors, and this is correct, in general, for operating systems. Operating Systems can execute different threads in different cores. But Python (at least the more common interpreters), ONLY CAN EXECUTE ONE THREAD AT THE SAME TIME INSIDE A PROCESS. That is because of the GIL (Global Interpreter Lock). So, a process Python (for example, the process that runs the interpreter), is not able to use multiple cores for running different Threads.
Vosk transcriptions are CPU-intensive tasks, and because of that, you cannot take advantage of multiple cores with multithreading. This does not mean that the Operating System stops switching between threads, only that you do not perceive an improved performance.
The solution is to make use of multiprocessing. You can see in this project how you can use a pool of processes with vosk to parallelize tasks. In the code I process mp3 files, but it would be easy to process audio frames from different mics in multiple processes while other processes make other exciting things. Communication between them could be done with multiprocessing queues, pipes or manager objects (queues would be OK).

- 614
- 5
- 9