1

我正在开发一个允许人们上传图片的网站。主要是学习经验,我是用python写的。它应该能够做的一件事是获取图像所在的 url,然后将其重新上传到自己的服务器。那么问题来了:

一个人给我的网站一个网址,然后我的服务器开始下载图像。当它正在下载该图像时,另一个请求进来了。有没有办法让服务器给出响应(比如说服务器很忙),即使它正在做其他事情?有没有更好的方法来解决这个问题?

提前致谢!

4

1 回答 1

0

我写了一个例子,希望对你有所帮助。它由一个urls.txt列出要处理的作业的文件和一个使用多个进程下载的 xdownloader.py 文件组成。我在运行 Python 2.7 的 Linux 机器上对其进行了测试。

xdownloader.py

from multiprocessing import Process
import urllib2

def main():

  try:

    # Read URLs/Destinations from a file
    jobs = []
    with open("urls.txt","r") as ifile:
      for line in ifile:
        jobs.append(line.split(" "))

    # Create a process list to keep track of running processes
    process_list = []
    # Iterate through our jobs list
    for url, save_to in jobs:
      # Create a new process that runs function 'download'
      p = Process(target=download, args=(url, save_to))
      # Save it to a list
      process_list.append(p)
      # Start the process
      p.start()
  except KeyboardInterrupt:
    print("Received keyboard interrupt (ctrl+c). Exiting...")
  finally:
    # Wait for all processes to finish before exiting
    for process in process_list:
       # Wait for this process to finish
       process.join()
    print("All processes finished successfully!")


def download(url, destination):
  # Open a request
  request = urllib2.urlopen(url)
  # Read and save the webpage data to a file
  with open(destination, "w+") as save_file:
    print("Downloading {0}".format(url))
    save_file.write(request.read())

if __name__=="__main__":
  main()

网址.txt

http://google.com google.html
http://yahoo.com yahoo.html
http://news.google.com news.google.html
http://reddit.com reddit.html
http://news.ycombinator.com news.ycombinator.html

运行python xdownloader.py我得到:

mike@localhost ~ $ python xdownloader.py 
Downloading http://news.ycombinator.com
Downloading http://reddit.com
Downloading http://news.google.com
Downloading http://google.com
Done downloading http://google.com
Done downloading http://news.ycombinator.com
Done downloading http://reddit.com
Downloading http://yahoo.com
Done downloading http://news.google.com
Done downloading http://yahoo.com

您可以看到这些作业正在异步运行……有些作业开始得早,但完成得比其他作业晚。(我在看你 news.google.com!)如果这个例子不符合你的需要,请在评论中告诉我。

于 2013-01-16T06:29:02.003 回答