Program uses the threading module but I didn’t notice any parallelism in the execution. What’s wrong?

Asked

Viewed 124 times

0

I made a script to decrease the quality of some mp3, calling the ffmpeg program through the subprocess module. I added Threads thinking of doing the process in parallel to multiple files at the same time. The program works but I have not seen any parallelism. I kept following the execution of the program and observed that, apparently, it is "converted" one MP3 at a time. Where is the parallelism? What I did wrong?

I also noticed that, running by IDLE, the '"released" prompt although the program continues running, that is, I can’t know when it finished...

import subprocess

from threading import Thread


def ConverteMusica(a):

    input_file_fmt = '{}.mp3'
    output_file_fmt = a

    for x in range(1, 5):
        subprocess.call(['ffmpeg',
                         '-i',
                         input_file_fmt.format(x),
                         '-acodec',
                         'libmp3lame',
                         '-ac',
                         '2',
                         '-ab',
                         '16k',
                         '-ar',
                         '44100',
                         output_file_fmt.format(x)])


MeuConversor1 = Thread(target=ConverteMusica,args=["Personalidades_Famosas_Elogiam_Cultura_Vedica.mp3"])
MeuConversor2 = Thread(target=ConverteMusica,args=["Sri_Caitanya_caritamrta_Madhya_lila_20_378_Aula_100.mp3"])
MeuConversor3 = Thread(target=ConverteMusica,args=["Sri_Caitanya_caritamrta_Madhya_lila_22_1_Aula_101.mp3"])
MeuConversor4 = Thread(target=ConverteMusica,args=["Sri_Caitanya_caritamrta_Madhya_lila_22_66_Aula_102.mp3"])


MeuConversor1.start()
MeuConversor2.start()
MeuConversor3.start()
MeuConversor4.start()
  • 1

    It is not generally recommended to use threads in Python, because they do not increase program performance in most cases. This is due to the GIL. Instead, use the module multiprocessing, which works quite similar to threads but circumvents the GIL problem by creating several separate Python processes. Also, I recommend using a Pool, so you don’t have to have a variable for each process.

  • Related or even duplicate because at the end the answer is this: https://answall.com/q/1946/101, which conforms Pedro’s comment. In the case of Python probably GIL makes it impracticable to have some gain in almost every situation.

  • @Maniero: It’s always better to use multiprocessing instead of threads, so?

  • 1

    Not necessarily, but in Python it usually is. The advantage of the thread is to share state while having more than one processing line, but you must ensure that you never have 2 lines accessing the data at the same time unless both are read-only. This is not simple and whenever it is done artificially by a lock potentially prevents parallel processing, which is what GIL ends up doing. In a separate process it does not share status, so everything goes fine. If you can do this, do it. But if you have to copy all the data from one process to another, it doesn’t pay

  • @Maniero: would that be so? Pastebin.com/Tz4ywj77 Apparently, my program is in looping!

  • I don’t have specific experience doing this in Python, so I can’t say.

  • @Pedrovonhertwig: Pastebin.com/Tz4ywj77 Apparently, my program is looping!

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.