import time
import asyncio
import telegram
from concurrent.futures import ThreadPoolExecutor
def send_image_sync(message):
try:
bot = telegram.Bot(message[0])
with open(message[2], 'rb') as f:
bot.sendPhoto(chat_id=int(message[1]), photo=f, caption=message[3])
except:
return False
return True
async def send_image_async(message):
try:
bot = telegram.Bot(message[0])
with open(message[2], 'rb') as f:
bot.sendPhoto(chat_id=int(message[1]), photo=f, caption=message[3])
except:
return False
return True
async def process_async(messages):
start = time.time()
futures = [asyncio.ensure_future(send_image_async(message)) for message in messages]
result = await asyncio.gather(*futures)
print(result)
end = time.time()
print(f'>>> 비동기 처리 총 소요 시간: {end - start}')
def process_sync(messages):
start = time.time()
result = []
for message in messages:
r = send_image_sync(message)
result.append(r)
print(result)
end = time.time()
print(f'>>> 동기 처리 총 소요 시간: {end - start}')
def process_threading(messages):
start = time.time()
with ThreadPoolExecutor(max_workers=len(messages)) as pool:
result = list(pool.map(send_image_sync, messages))
print(result)
end = time.time()
print(f'>>> 스레딩 처리 총 소요 시간: {end - start}')
if __name__ == '__main__':
messages = []
messages.append(('5037968073:AAGL99URHDwbzUqmu3uQE7cWt9ggb9kvgPY', '-1001504056601', 'imgs/sample.png', '테스트다'))
messages.append(('5036482104:AAH92uHXjLVY-Cr4SXZkNFUiufx-jZX7bE4', '-1001589225080', 'imgs/sample.png', '테스트다'))
messages.append(('5036482104:AAH92uHXjLVY-Cr4SXZkNFUiufx-jZX7bE4', '-764437519', 'imgs/sample.png', '테스트다'))
process_sync(messages)
asyncio.run(process_async(messages))
process_threading(messages)
[True, True, True]
>>> 동기 처리 총 소요 시간: 7.4156105518341064
[True, True, True]
>>> 비동기 처리 총 소요 시간: 7.327911376953125
[True, True, True]
>>> 스레딩 처리 총 소요 시간: 2.477998971939087
스레드 아이디 알아오기
from concurrent.futures import ThreadPoolExecutor
from time import sleep
from threading import current_thread
import random
def func(param):
sleep(0.1 * random.randint(0, 10))
thread_id = int(current_thread().name.split("_")[-1])
print(param, thread_id)
return True
def main():
params = [e for e in range(8)]
with ThreadPoolExecutor(max_workers=4) as pool:
r = pool.map(func, params)
if __name__ == '__main__':
main()