-1

Here is my code:

   import os
import requests
from bs4 import BeautifulSoup
import time, random
from time import sleep
from concurrent.futures import ProcessPoolExecutor, as_completed
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import Future 
import concurrent.futures
from threading import Thread

def task():
    start_time = time.time()

    with open("url.txt") as f:
        for line in f:
            line2 = line.rstrip("\r\n")
            print(line2 + " Hello")
            sleep(1)
            #rechi = requests.get("http://"+line2, verify = False, timeout = 0.5)
            #con = BeautifulSoup(rechi.content, "html.parser")
            #title = con.title
            #print(title)

    print("My program took", time.time() - start_time, "to run")

def main():
    executor = ThreadPoolExecutor(20)
    future = executor.submit(task)
    print(future.result())

main()

The time it takes to execute the program with threading is the same as without threading... Can somebody tell me what am i doing wrong please?

Zarovsky
  • 21
  • 4

1 Answers1

0

Multithreading only makes your program faster if you run multiple tasks in parallel. Your program runs task once in a single Thread. Even if you had task running multiple times in parallel the time that your program outputs is only the time needed to complete task once and not the time needed for the whole program to run. This prints the time for main and submits task multiple times:

def main():
   start_time = time.time()
   executor = ThreadPoolExecutor(20)
   futures = []
   futures.append(executor.submit(task))
   futures.append(executor.submit(task))
   for i in futures:
       print(i.result())
   print("main took {} seconds".format(time.time()-start_time))
Lcj
  • 371
  • 3
  • 10