DBILITY

python multi-threading ( 멀티쓰레드 ) 본문

python

python multi-threading ( 멀티쓰레드 )

DBILITY 2021. 8. 17. 15:18
반응형

Mock up서버는 훌륭한 분이 개발을 해두어서 사용했다.

https://github.com/joon610/mockup-server

 

GitHub - joon610/mockup-server: this is mock server (Vue + TypeScript + Electron)

this is mock server (Vue + TypeScript + Electron) - GitHub - joon610/mockup-server: this is mock server (Vue + TypeScript + Electron)

github.com

Fetch데이터는 아래에서 받았다.

https://dummy.restapiexample.com/

 

Dummy sample rest api - dummy.restapiexample.com

Welcome Dummy api example Info - Its a free and Public API, There are some people are using CRON job to insert and update.Please avoid CRON job that will cause Server issue. I am not making enough money to get VPS. I have changed results Data structure, I

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."
}

index.json
0.00MB

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
반응형
Comments