All the requests are working normally but after 90% of the tasks the responds get hung and timeout.
In the following code, if you change n_requests to 1000 or 10 the problem is still there and it seems always to be 10% which wont get a response.
It only seems to work with 2 requests (n_requests = 2):
import asyncio
import aiohttp
n_requests = 100
async def make_request(session, req_n):
url = f"https://tbit-web.de/exec/ximg.php?fid={req_n}"
print(req_n)
async with session.get(url) as resp:
print(req_n, "finish")
async def main():
async with aiohttp.ClientSession() as session:
counternr = 0
while counternr < 9999:
await asyncio.gather(
*[make_request(session, i) for i in range(counternr, counternr + n_requests)]
)
counternr= n_requests + counternr
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Related
so i tried to make a spam function and run it but it just gives this error: RuntimeWarning: coroutine 'spam' was never awaited
spam(channel)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
and no message is being sent.
this is the code:
await guild.create_text_channel("NEEDLESS-NUKER")
for channel in guild.text_channels:
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
amount = 500
for i in range(amount):
await guild.create_text_channel(random.choice(SPAM_CHANNEL))
spam(channel)
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
print(f"{c.YELLOW}[{date}]{c.GREEN} Nuked {guild.name} Successfully.")
return
async def spam(channel):
for i in range(15):
try:
names = json.loads(open("words.json").read())
SPAM_MESSAGE = [
"#everyone LOL GET NUKED :skull::skull:",
"#everyone LOL IMAGINE GETTING NUKED :skull::skull:",
"lol hahaha get nuked",
f"im {random.choice(names)}",
f"Lol this is so random like {random.choice(names)}#{random.randint(1, 1000)} :skull:",
f"Made By {random.choice(names)}!",
]
msg = random.choice(SPAM_MESSAGE)
await channel.send(msg)
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
print(f"{c.YELLOW}[{date}]{c.GREEN} Succeffully send {msg}")
except Exception as e:
names = json.loads(open("words.json").read())
SPAM_MESSAGE = [
"#everyone LOL GET NUKED JOIN https://dsc.gg/pythondevs",
"#everyone LOL IMAGINE GETTING NUKED :skull::skull:",
"#everyone https://dsc.gg/pythondevs",
f"#everyone im {random.choice(names)}",
f"#everyone Lol this is so random like {random.choice(names)}!{random.randint(1000, 10000)}",
f"#everyone Made By {random.choice(names)}!",
]
msg = random.choice(SPAM_MESSAGE)
date = str(datetime.now()).split(".")[0]
date = date.split(" ")[1]
print(f"{c.YELLOW}[{date}]{c.RED} Failed Sending Message {msg} exc: {e}")
first it was #client.event and then on_guild_channel_create(channel) and it worked but now it wont work i changed it because everytime i created a channel with this bot it would send 15 messages in the channel and if i make a voice channel it just gives me an error.
so please help me fix this. Thanks.
You're calling an async function, so you need to create_task it or await it.
import asyncio
import time
async def wait:
time.sleep(1)
await asyncio.sleep(1)
return None
result = asyncio.create_task(wait()) #Wait 1 second.
await wait() #Wait 2 seconds because it will wait for execution to end.
In your case, you would need to change line 8 from (8th line of the answer)
spam(channel)
to
await spam(channel)
# https://bot.sannysoft.com/
from pyppeteer import *
from pyppeteer_stealth import stealth
import asyncio
# --------------------------------------------------------------
def handle_price(some_price):
for element in some_price:
try:
int(element)
except:
some_price = some_price.replace(element, '')
return some_price
#-----
async def get_wildberries_price(browser):
page = await browser.newPage()
await page.goto('https://www.wildberries.ru/catalog/98649739/detail.aspx')
await page.waitFor('.price-block__final-price')
raw_price = await page.evaluate('''
() => {
return document.querySelector('.price-block__final-price').textContent
}
''')
final_price = handle_price(raw_price)
await page.screenshot({'path': 'wildberries.png'})
return final_price
#-----
async def get_ozon_price(browser):
page = await browser.newPage()
await stealth(page)
await page.goto('https://www.ozon.ru/product/setka-sadovaya-plastikovaya-0-61-sm-opornaya-dlya-rasteniy-rulon-5-m-yacheyka-7-10-mm-zelenaya-746399567/?advert=LMWD3jhEugrU0NaiTNSLwpAIIbCdFMNn7pzKseZ54oiYJzffMkWL5dFUOvP2QxFxTe6r-62ds5iYdNtmm39GeVmWNx7Drk2RfA-NFruujb9wW_wbefQotjVI3Nw6nmw-7bz9eBuQf1jcWO9oKG03ATRZlVPYDWDUpt-Vr6TP01QKzNpkDGeyKNFDYr0mQ6zHtTc2xumWB3-dv2Gz-N1MubGGYv1HNBGFFtjS2sjma9sLgVekajUUaWojBH262l4QrAW1M9rCl-81e2UuCuA6e9e30jQi6V5VQGajDtoh7hlSqVBeuWTgQwBh93dIZprdLVVLO8wjhyroY4zH5kCXsPosmppupEfHuVI3WyxszZDRKZaftEvaKMKR1BDr1rtJDPW_pNvM1ZhWuCyJp9hsDQsED8fQ3RFRWR8LNZ5WYr8h_sjfYw1xlrl5-rjcS2MaBuBeYL3GQ4pXFwrkRKOEFuLsDScNPdIL2rDN9aeFT_lW5QnmTG_L177BU5QyZM2DLu7pN8H3K0To7QDSzvLpfVqCHWaudAT54N6mTU8GAtwBLp6sWpQNKDwnqqFLxmdC1s-3-U2t3h6jwkvEH5nue2hZdmrdu4Z-eD2qbnblV340tMRQZ3V8vcw-QO0BiDqNQupojM-i61GULUdv8a8qa12Uir-HZ1OOaKekgidc-IKoCUbTCa8YfpV5zXASOBlxvqs84FoG8vEg2yNkN4fRSOOkfcSp29rssttskpEkp14dZ3o_XnJ93SI1SR13ffRmFMGVZiwDjOyHjovEELGadA&avtc=1&avte=2&avts=1674570447&keywords=%D0%BF%D0%BB%D0%B0%D1%81%D1%82%D0%B8%D0%BA%D0%BE%D0%B2%D0%B0%D1%8F+%D1%81%D0%B5%D1%82%D0%BA%D0%B0+%D1%81%D0%BB%D0%B0%D0%B2&sh=6RhLQaLOhA')
await page.waitFor('[slot="content"] span')
raw_price = await page.evaluate('''
() => {
return document.querySelector('[slot="content"] span').textContent
}
''')
final_price = handle_price(raw_price)
await page.screenshot({'path': 'ozon.png'})
return final_price
# --------------------------------------------------------------
async def main():
main_browser = await launch({
# 'headless': False,
})
print(await get_wildberries_price(main_browser))
print(await get_ozon_price(main_browser))
await main_browser.close()
main_loop = asyncio.new_event_loop()
main_loop.run_until_complete(main())
Error: (Future exception was never retrieved
future:<Future finished exception=NetworkError('Protocol error Target.sendMessageToTarget: Target closed.')>
pyppeteer.errors.NetworkError: Protocol error Target.sendMessageToTarget: Target closed)
This error appears not often (about 1 time in a 4 starts).
But I noticed if I change places
print(await get_wildberries_price(main_browser))
print(await get_wildberries_price(main_browser))
code will be OK
I don't understand why place is important
I beg, help me, nothing works
My Discord bot allows users to play a song starting from a timestamp.
The problem is that playback is delayed and audio plays faster and is jumbled if start times >= 30s are set.
Results from testing different start times. Same URL, 30 second duration:
Entered Start Time (s)
Playback Delay (s)
Song Playback Time (s)
0
3
30
30
10
22
60
17
17
120
31
2
150
120
<1
I am setting the start time using ffmpeg_options as suggested in this question.
Does anyone understand why the audio playback is being delayed/jumbled? How can I improve playback delay and allow users to start in the middle of a multi-chapter YouTube video?
Code:
import discord
import youtube_dl
import asyncio
# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ""
ytdl_format_options = {
"format": "bestaudio/best",
"outtmpl": "%(extractor)s-%(id)s-%(title)s.%(ext)s",
"restrictfilenames": True,
"noplaylist": False,
"yesplaylist": True,
"nocheckcertificate": True,
"ignoreerrors": False,
"logtostderr": False,
"quiet": True,
"no_warnings": True,
"default_search": "auto",
"source_address": "0.0.0.0", # Bind to ipv4 since ipv6 addresses cause issues at certain times
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source: discord.AudioSource, *, data: dict, volume: float = 0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get("title")
self.url = data.get("url")
#classmethod
async def from_url(cls, url, *, loop=None, stream=False, timestamp = 0):
ffmpeg_options = {
"options": f"-vn -ss {timestamp}"}
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if "entries" in data:
# Takes the first item from a playlist
data = data["entries"][0]
filename = data["url"] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
intents = discord.Intents.default()
bot = discord.Bot(intents=intents)
#bot.slash_command()
async def play(ctx, audio: discord.Option(), seconds: discord.Option(), timestamp: discord.Option()):
channel = ctx.author.voice.channel
voice = await channel.connect()
player = await YTDLSource.from_url(audio, loop=bot.loop, stream=True, timestamp=int(timestamp))
voice.play(player)
await asyncio.sleep(int(seconds))
await voice.disconnect()
token = token value
bot.run(token)
async def simultaneous_chunked_download(urls_paths, label):
timeout = ClientTimeout(total=60000)
sem = asyncio.Semaphore(5)
async with aiohttp.ClientSession(timeout=timeout, connector=aiohttp.TCPConnector(verify_ssl=False)) as cs:
async def _fetch(r, path):
async with sem:
async with aiofiles.open(path, "wb") as f:
async for chunk in r.content.iter_any():
if not chunk:
break
size = await f.write(chunk)
if not indeterminate:
bar._done += size
bar.show(bar._done)
if indeterminate:
bar._done += 1
bar.show(bar._done)
indeterminate = False
total_length = 0
tasks = []
for url, path in urls_paths.items():
r = await cs.get(url)
if not indeterminate:
try:
total_length += r.content_length
except Exception:
indeterminate = True
tasks.append(_fetch(r, path))
verbose_print(f"url: {url},\npath: {path}\n\n")
if not indeterminate:
bar = progress.Bar(
expected_size=total_length, label=label, width=28, hide=False
)
else:
bar = progress.Bar(
expected_size=len(tasks), label=label, width=28, hide=False
)
logger._pause_file_output = True
bar.show(0)
bar._done = 0
await asyncio.gather(*tasks)
logger._pause_file_output = False
bar.done()
The function I have above is for downloading a dictionary of urls asynchronously and then printing out a progress bar. An example of its usage:
The code itself runs perfectly fine, however i keep getting these errors:
Whilst benign, they are an eyesore and could point towards my lack of knowledge on both http and asynchronous code, so i would rather try and get it fixed. However im at a loss on where or what is causing it, especially as i like i said the code runs perfectly fine regardless.
If you would like a more practical hands on attempt at recreating this the full code is on my github repo on the dev branch: https://github.com/ohitstom/spicetify-easyinstall/tree/dev
Most of the program can be disregarding if you are testing this out, just press the install button and the problematic code will show itself towards the end.
Bare in mind this is a spotify themer so if you have spotify/spicetify installed you will want to use a vm.
FIXED!:
# Create App
globals.app = QtWidgets.QApplication(sys.argv)
globals.app.setStyleSheet(gui.QSS)
# Configure asyncio loop to work with PyQt5
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
loop = QEventLoop(globals.app)
asyncio.set_event_loop(loop)
# Setup GUI
globals.gui = gui.MainWindow()
globals.gui.show()
# Set off loop
with loop:
sys.exit(loop.run_until_complete(globals.gui.exit_request.wait()))
class MainWindow(QuickWidget):
def __init__(self):
super().__init__(
name="main_window",
...etc
)
self.exit_request = asyncio.Event()
......etc
def closeEvent(self, *args):
self.exit_request.set()
Asyncio and aiohttp have some problems when running a lot of tasks concurrently on Windows, I've been having a lot of problems with it lately.
There are some workarounds available, the ones I use most are:
# set this before your event loop initialization or main function
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Or:
loop = asyncio.ProactorEventLoop()
asyncio.set_event_loop(loop)
loop.run_until_complete(your_main())
I try to connect two M5StickC to PC via BLE to upload their sensor data.
I wrote a data acquisition python script using bleak library.
The data acquisition rate is very slow when I connect 2 devices.
How to improve data acquisition rate on my script ?
I hope to get 20 data per second.
import asyncio
from bleak import BleakClient
address1 = "D8:A0:1D:55:EE:8A"
UUID1 = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
address2 = "94:B9:7E:93:21:76"
UUID2 = "beb5483e-36e1-4688-b7f5-ea07361b26a2"
async def main():
client1 = BleakClient(address1)
client2 = BleakClient(address2)
print(client1.address)
print(client2.address)
await client1.connect()
# await client2.connect()
while(True): # very slow when comment out client2
print(await client1.read_gatt_char(UUID1))
# print(await client2.read_gatt_char(UUID2))
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
edit:
Thank you for your comments.
I have updated my script following ukBaz's link.
Data upload rate is much improved, but I could get almost only single M5StickC data, the data from another one available sparsely...
I will ask this question to bleak maintainer.
from bleak import BleakClient
import asyncio
address1 = "D8:A0:1D:55:EE:8A"
UUID1 = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
address2 = "94:B9:7E:93:21:76"
UUID2 = "beb5483e-36e1-4688-b7f5-ea07361b26a2"
def callback(sender, data):
print(sender, data)
def run(addresses, UUIDs):
loop = asyncio.get_event_loop()
tasks = asyncio.gather(*(connect_to_device(address, UUID) for address, UUID in zip(addresses, UUIDs)))
loop.run_until_complete(tasks)
async def connect_to_device(address, UUID):
print("starting", address, "loop")
async with BleakClient(address, timeout=5.0) as client:
print("connect to", address)
while(True):
try:
print(await client.read_gatt_char(UUID))
except Exception as e:
print(e)
if __name__ == "__main__":
run([address1, address2], [UUID1, UUID2])