Advertisement
ShinkaiShoujo

threading and multiprocessing

May 27th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1.  
  2. import threading
  3. from urllib import urlopen
  4. import json
  5.  
  6. import time
  7.  
  8. #f = urlopen('http://fucking-great-advice.ru/api/random')
  9. #s = f.read()
  10. #json_s = json.loads(s)
  11. #print(json_s['text'])
  12.  
  13. import requests
  14. lock = threading.Lock()
  15. def get_quote(i):
  16.     f = requests.get('http://fucking-great-advice.ru/api/random')
  17.     json_s = f.json()
  18.     lock.acquire()
  19.     #print(str(i)+ ') ' + json_s['text'])
  20.     lock.release()
  21.  
  22. import multiprocessing
  23. thread_list = []
  24. n = 1000
  25. start_time = time.time()
  26. for i in range(0,n):
  27.     p = multiprocessing.Process(target=get_quote,args=(i,))
  28.     thread_list.append(p)
  29.     thread_list[i].start()
  30.  
  31. for i in range(0,n):
  32.     thread_list[i].join()
  33. print(time.time() - start_time)
  34.  
  35. thread_list = []
  36. start_time = time.time()
  37. for i in range(0,n):
  38.     thread_list.append(threading.Thread(target=get_quote, args=(i,)))
  39.     thread_list[i].start()
  40. for i in range(0,n):
  41.     thread_list[i].join()
  42. print(time.time() - start_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement