Compare commits

..

2 Commits

Author SHA1 Message Date
703feb8ca3 Fix max thread to 10 because au pool size
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2023-11-02 21:42:14 +01:00
bc5683cd72 Fix async blocking state 2023-11-02 21:41:53 +01:00
2 changed files with 25 additions and 6 deletions

View File

@ -1,3 +1,4 @@
import asyncio
import logging
import os
from shlex import join
@ -55,8 +56,13 @@ async def pixivprev(interaction: discord.Interaction, url: str):
if result and match is not None:
await interaction.response.defer()
with TemporaryDirectory() as tmp_dir:
data = pixiv_api.dowload_pixiv_images(
int(match.group("id")), tmp_dir, REFRESH_TOKEN
loop = asyncio.get_running_loop()
data = await loop.run_in_executor(
None,
pixiv_api.dowload_pixiv_images,
int(match.group("id")),
tmp_dir,
REFRESH_TOKEN,
)
if data is None:
await interaction.response.send_message(
@ -86,8 +92,13 @@ async def pixiv_preview(interaction: discord.Interaction, message: discord.Messa
if result and match is not None:
await interaction.response.defer()
with TemporaryDirectory() as tmp_dir:
data = pixiv_api.dowload_pixiv_images(
int(match.group("id")), tmp_dir, REFRESH_TOKEN
loop = asyncio.get_running_loop()
data = await loop.run_in_executor(
None,
pixiv_api.dowload_pixiv_images,
int(match.group("id")),
tmp_dir,
REFRESH_TOKEN,
)
if data is None:
await interaction.response.send_message(
@ -126,8 +137,13 @@ async def on_message(message: discord.Message):
if result and match is not None:
with TemporaryDirectory() as tmp_dir:
async with message.channel.typing():
data = pixiv_api.dowload_pixiv_images(
int(match.group("id")), tmp_dir, REFRESH_TOKEN
loop = asyncio.get_running_loop()
data = await loop.run_in_executor(
None,
pixiv_api.dowload_pixiv_images,
int(match.group("id")),
tmp_dir,
REFRESH_TOKEN,
)
if data is None:
return

View File

@ -9,6 +9,8 @@ import pixivpy3
cpu_count = os.cpu_count()
max_thread_num = cpu_count * 2 if cpu_count is not None else 4
if max_thread_num > 10:
max_thread_num = 10
def dowload_pixiv_images(
@ -22,6 +24,7 @@ def dowload_pixiv_images(
json_result = api.illust_detail(illust_id)
illust = json_result.illust
if not illust["visible"]:
logging.info("Can't be viewed, return None")
return None
pages = illust["meta_pages"]
if len(pages) > 0: