Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- SSL
- Sqoop
- mapreduce
- SPC
- tomcat
- hadoop
- 공정능력
- JavaScript
- Android
- react
- plugin
- MSSQL
- xPlatform
- 보조정렬
- mybatis
- Spring
- Kotlin
- IntelliJ
- GIT
- NPM
- Python
- Java
- SQL
- Express
- vaadin
- window
- table
- R
- es6
- Eclipse
Archives
- Today
- Total
DBILITY
python multi-threading ( 멀티쓰레드 ) 본문
반응형
Mock up서버는 훌륭한 분이 개발을 해두어서 사용했다.
https://github.com/joon610/mockup-server
Fetch데이터는 아래에서 받았다.
https://dummy.restapiexample.com/
fetch용 데이터
{
"status": "success",
"data": [
{
"id": 1,
"employee_name": "Tiger Nixon",
"employee_salary": 320800,
"employee_age": 61,
"profile_image": ""
},
........
],
"message": "Successfully! All records has been fetched."
}
import json
import math
import threading
import time
from multiprocessing.dummy import Pool as ThreadPool
import random
import requests
urls: list = [
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01',
'http://localhost:9000/test01'
]
def get_data(url):
rtval: int = 0
try:
req = requests.get(url)
req.raise_for_status()
if req.status_code == 200:
req_data = json.loads(req.content)
idx = random.sample(range(0,23),k=1)
rtval = req_data['data'][idx[0]]['employee_name']
else:
print(f'status_code -----> {req.status_code}')
except IOError as irr:
print(f'IOError -----> {irr}')
except OSError as orr:
print(f'OSError -----> {orr}')
finally:
print(threading.get_ident(), threading.currentThread().getName())
return rtval
#get_data(urls[0])
#exit(0)
start_time: float = time.time()
pool = ThreadPool(4)
result: list = pool.map(get_data, urls)
pool.close()
pool.join()
print(result)
end_time: float = time.time()
print(start_time, end_time, end_time - start_time)
print(str(math.floor((end_time - start_time) * 1000)) + 'ms')
실행결과
14532 Thread-2
1308 Thread-1
4120 Thread-4
5560 Thread-3
14532 Thread-2
1308 Thread-1
4120 Thread-4
5560 Thread-3
14532 Thread-2
1308 Thread-1
['Tatyana Fitzpatrick', 'Brielle Williamson', 'Gloria Little', 'Tiger Nixon', 'Airi Satou', 'Caesar Vance', 'Garrett Winters', 'Jena Gaines', 'Bradley Greer', 'Colleen Hurst']
1634283798.2044365 1634283798.3309457 0.1265091896057129
126ms
Process finished with exit code 0
반응형
'python' 카테고리의 다른 글
python class (0) | 2021.08.19 |
---|---|
python file rename, copy etc. (0) | 2021.08.19 |
python deal with time ( 시간 다루기 ) (0) | 2021.08.17 |
python string placeholder? formatting (0) | 2021.08.17 |
python lottery number generation exercise ( 로또 번호 생성) (0) | 2021.08.13 |
Comments