Search code examples
python-3.xpython-multithreading

How to solve winerror32 while using threading?


I am currently using python to download a mp4 file from the internet, convert it to mp3 then delete the mp4 file. I wanted to run this process 3 at a time so I used threading. However, it gave me winerror32 when I was accessing the folder where the mp4 was downloaded.

I think it because one thread is accessing the folder at the same time as the other thread. Is there way to force the thread to access the folder one at a time?

I tried using lock to only allow one thread at a time but it did not work

def download_multi(music,links,check,parent_file,default_filename,success,fail,total):
    try:
        lock = threading.Lock()
        lock.acquire()
        print(format(threading.currentThread().getName()))
        print("Downloading- ", str(check), "/", total, default_filename + "...")
        check += 1
        lock.release()
        # downloads first audio stream
        music.download(parent_file)
        # creates mp3 filename for downloaded file
        new_filename = default_filename[0:-3] + "mp3"
        print("\nConverting to mp3....")
        # converts mp4 audio to mp3 audio
        lock.acquire()
        audioclip = VideoFileClip(os.path.join(parent_file, default_filename))
        audioclip.audio.write_audiofile(os.path.join(parent_file, new_filename))
        # print("Download finished.")
        # end the video process
        audioclip.close()
        # erase mp4
        if os.path.isfile(parent_file + "\\" + default_filename):
            os.remove(parent_file + "\\" + default_filename)
            print(default_filename + "delete complete\n")

            # list that would be added to log file
            success.append(default_filename)
        lock.release()
    except Exception as e:
        print(e)
        print("unable to download " + default_filename)
        # list that would be added to log file
        fail.append(default_filename)
        pass

Solution

  • The problem was that I was passing wrong parameters to the method. The code itself does not have a problem