只有1个线程启动for循环

时间:2015-12-05 09:06:56

标签: python multithreading python-2.7 for-loop

所以我试图使用Python 2.7编写一个非常简单的Internet Download Manager欺骗

应该根据字节范围查询文件HTTP头,获取字节范围并在no.of线程(为简单起见,我硬编码2)之间传播下载,然后再将文件部分连接在一起。

问题是我的控制台日志告诉我只启动了1个线程。

[编辑]问题已经解决。找到下面的工作代码。

这是我的来源:

{'start': 0, 'end': 527868}
{'start': 0, 'end': 527868}
Thread 1 started
Start at_0Ends at_515
1_0_515
1_0_515
Finito!
Download took_6.97844422658

输出:

from __future__ import print_function

import threading
import urllib
import urllib2

import time

threads = []
parts = {}

# url to open
url = "http://www.sample-videos.com/audio/mp3/india-national-anthem.mp3"
u = urllib.urlopen(url)

# define file
file_name = "test.mp3"
f = open(file_name, 'wb')


# open url and get header info
def get_file_size(url):
    stream_size =  u.info()['Content-Length']
    file_size = stream_size
    return file_size

start = 0
#get stream size
end = get_file_size(url)
# specify block size
block_sz = 512

#algo to divide work among 2 threads
def calculate_no_of_bytes_for_thread1():
    full_stream_size = end
    first_thread = {'start':0, 'end':(int(full_stream_size)/2)}
    print(first_thread)
    return first_thread

#algo to divide work among 2 threads
def calculate_no_of_bytes_for_thread2():
    full_stream_size = end
    second_thread= {'start':int(full_stream_size)/2,'end': int(full_stream_size)}
    print(second_thread)
    return second_thread



# download function
def download_thread(url ,id,start,end):
    current_size = int(float(start)/1024)
    total_size = int(float(end)/1024)
    print ("Start at_"+str(current_size) + "Ends at_" + str(total_size))

    # specify request range and init stream
    req = urllib2.Request(url)
    req.headers['Range'] = 'bytes=%s-%s' % (start, end)

    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break
        start += len(buffer)
        f.write(buffer)
        thread_id = id
        status =  "Thread ID_" +str(thread_id) + "Downloaded_" + str(int(start/1024)) + "Total_" +str(total_size)
        print (status)

#starts 2 threads
def start_threads():
    for i in range(2):
        #if first loop, start thread 1
        if(i==0):
            start = calculate_no_of_bytes_for_thread1().get('start')
            end = calculate_no_of_bytes_for_thread1().get('end')
            print("Thread 1 started")
            t = threading.Thread(target=download_thread, args=(url,i,start,end))
            t.start()
            threads.append( t)
        #if second loop, start thread 2
        if(i==1):
            start = calculate_no_of_bytes_for_thread2().get('start')
            end = calculate_no_of_bytes_for_thread2().get('end')
            print("Thread 2 started")
            t = threading.Thread(target=download_thread, args=(url,i,start,end))
            t.start()
            threads.append( t)

    # Join threads back (order doesn't matter, you just want them all)
    for i in threads:
       i.join()

    # Sort parts and you're done
    # result = ''
    # for i in range(2):
    #     result += parts[i*block_sz]

#start benchmarking
start_time = time.clock()

start_threads()

print ("Finito!")

end_time = time.clock()
benchmark = str(end_time - start_time)
print ("Download took_" +benchmark)

f.close()

工作代码:

.eathealthy-section {
/* height: 75%; */
padding-top: 75px; 
text-align: center;
background: #eee;
}

1 个答案:

答案 0 :(得分:1)

你有:

for i in range(2):
    if(i==1):
        ...
    if(i==2):
        ...

但是range(2)遍历[0,1]而不是[1,2]。

省去一些麻烦,只需删除这3行。启动两个线程的代码可以串行运行。