File downloading using python with threads

后端 未结 4 799
一生所求
一生所求 2021-01-03 19:09

I\'m creating a python script which accepts a path to a remote file and an n number of threads. The file\'s size will be divided by the number of threads, when each thread c

4条回答
  •  执念已碎
    2021-01-03 19:25

    You can use a thread safe "semaphore", like this:

    class Counter:
      counter = 0
      @classmethod
      def inc(cls):
        n = cls.counter = cls.counter + 1 # atomic increment and assignment
        return n
    

    Using Counter.inc() returns an incremented number across threads, which you can use to keep track of the current block of bytes.

    That being said, there's no need to split up file downloads into several threads, because the downstream is way slower than the writing to disk, so one thread will always finish before the next one is downloading.

    The best and least resource hungry way is simply to have a download file descriptor linked directly to a file object on disk.

提交回复
热议问题