Title: Performing multiple tasks at the same time in Python
Tags: python;selenium
Question: The following code can download one file from one given url at a time:

```from selenium import webdriver

with open("url_lists.txt","r") as fi:  ###The text file contains hundreds of urls
    urls = fi.read().splitlines()

for url in urls:                    
    browser = webdriver.Firefox()           
    browser.get(url)    
    browser.find_element_by_id('download').click()
```

I want to modify the code so that 5 urls are open simultaneously by 5 different browsers, and download all the 5 files at a time.

How can I accomplish it?

Comment: If my answer solves your problem, please confirm it. Thanks!

Here is the accepted answer: You can use ```threading```. 

```#!/usr/bin/env python
#-*- coding:utf-8 -*-

from selenium import webdriver
from threading import Thread

with open("url_lists.txt","r") as fi:  ###The text file contains hundreds of urls
    urls = fi.read().splitlines()

def func(url, bro):

    browserFunc = getattr(webdriver, bro, webdriver.Firefox)
    browser = browserFunc()           
    browser.get(url)    
    browser.find_element_by_id('download').click()

t = []
urls = [1,2,3,4,5]
bros = [1,2,3,4,5]

for i in range(len(urls)):
    t.append(Thread(target=func, args=[urls[i], bros[i]]))
for i in t:
    t.start()
for i in t:
    t.join()


if __name__ == '__main__':

    a = test1()
```

Comment for this answer: Sorry, I don't get your point. You mean download from 5 urls from one browsers?Or?
Comment for this answer: How can I control resources and download only, for example, 5 urls at one time?
Comment for this answer: I mean downloading from 5 urls using ONLY 5 browsers at a time.

Here is another answer: Use can use gevent for this:

```from gevent import monkey
monkey.patch_all()

from gevent import spawn, joinall
from selenium import webdriver


def worker(url, worker_number):
    browser = webdriver.Firefox()
    print 'worker #%s getting "%s"' % (worker_number, url)
    browser.get(url)
    print 'worker #%s got "%s"' % (worker_number, url)


if __name__ == '__main__':
    print 'start'
    fh = open('url_lists.txt', 'rb')
    joinall([spawn(worker, url.strip(), i) for i, url in enumerate(fh.readlines())])
    fh.close()
    print 'stop'
```

This example will spawn as many threads (workers) as urls in the file. So if your file has too many urls it's better to use queues or pool of LIMIT number of workers to control resources and download only, for example, 50 urls at one time.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:07/16/14', 'FavoriteCount': 3.0, 'PostTypeId': 1, 'Tags': '<python><selenium>', 'Title': 'Performing multiple tasks at the same time in Python', 'ViewCount': 1752, 'id': 24776061, 'user_AboutMe': '', 'user_AccountId': 4720630.0, 'user_DisplayName': 'bas', 'user_Reputation': 25.0, 'user_Views': 2.0, 'user_id': 3818589.0}}