Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to handle timeouts#2876

AnsweredbyHappy-Ferret
Happy-Ferret asked this question inQ&A
Discussion options

Unfortunately, the Wiki doesn't provide any code samples on how to properly handle timeouts.

Usingechobot.py as an example, connections will sometimes be closed on the Telegram end and aNetworkError exception is raised, hence there needs to be a retry.

It's unclear how a retry can be achieved, alas.

This is what I tried, thus far. To no avail:

...deferror_handler(update:Update,context:CallbackContext)->None:print("Error raised")update.message.chat.bot.get_updates()time.sleep(10)dispatcher.add_error_handler(error_handler)...
You must be logged in to vote

This is the solution I ended up settling on.

defretry_on_error(func,wait=0.1,retry=0,*args,**kwargs):i=0whileTrue:try:returnfunc(*args,**kwargs)breakexcepttelegram.error.NetworkError:logging.exception(f"Network Error. Retrying...{i}")i+=1time.sleep(wait)ifretry!=0andi==retry:break

Which can then be used as follows:

retry_on_error(update.message.reply_photo,photo=url)

EDIT: Should bereturn func...

Replies: 3 comments 12 replies

Comment options

clot27
Jan 25, 2022
Collaborator

There's an wiki article forHandling network errors

You must be logged in to vote
9 replies
@Happy-Ferret
Comment options

Ok. So if I understand this correctly, the error handler is the wrong place to implement a retry logic, correct?
I guess I could write a@retry(retries: int, sleep: int) decorator.

I was hoping for a more general solution. Something akin to Telebot'sinfinity_polling.

@Bibo-Joshi
Comment options

Ok. So if I understand this correctly, the error handler is the wrong place to implement a retry logic, correct?

yes

I guess I could write a@retry(retries: int, sleep: int) decorator.

Note that this would retry the whole callback, which may be too much. e.g. if your callback sends 2 messages and only the second one fails, then aretry decorator would call the whole callback again, leading to the first message also being send again even though it was successful the first time already

I was hoping for a more general solution. Something akin to Telebot'sinfinity_polling.

You're mixing things up here:infinty_polling is a long running task tofetch updates, not to handle them.Updater.start_polling. PTBs equivalent isUpdater.start_polling, which is completely independent ofDispatcher, who manages the handlers

@Happy-Ferret
Comment options

Note that this would retry the whole callback, which may be too much. e.g. if your callback sends 2 messages and only the second one fails, then a retry decorator would call the whole callback again, leading to the first message also being send again even though it was successful the first time already

Yep. You're right. I thought about that right after I posted it. Lol.

Hm. I guess unless I'm overlooking something obvious, the best solution would be to wrap the most recurring API calls in my own functions. I'd rather not clutter my entire codebase with try-except logic.

You're mixing things up here: infinty_polling is a long running task to fetch updates, not to handle them. Updater.start_polling. PTBs equivalent is Updater.start_polling, which is completely independent of Dispatcher, who manages the handlers

Oh. Ok. I wasn't sure. I thought infinity_polling was basically a general solution that the creator of Telebot built out of PTB.

@Bibo-Joshi
Comment options

creator of Telebot built out of PTB.

not sure if I'm understanding you correctly, but pyTelegramBotAPI is not built on top of PTB - it's an independent library

@Happy-Ferret
Comment options

creator of Telebot built out of PTB.

not sure if I'm understanding you correctly, but pyTelegramBotAPI is not built on top of PTB - it's an independent library

My bad. I could've sworn I saw references to PTB.
I guess I was browsing with too many tabs. Lol

Comment options

Note thattime.sleep(10) will make your bot unresponsive for 10 seconds. Maybe that's what you want - maybe it isn't. Just pointing it out.

You must be logged in to vote
1 reply
@Happy-Ferret
Comment options

Note thattime.sleep(10) will make your bot unresponsive for 10 seconds. Maybe that's what you want - maybe it isn't. Just pointing it out.

That's what I figured. I just found it in another Telegram bot library that has aninfinity_polling method. It didn't make sense to me at the time but I added it for trial-and-error.

Comment options

This is the solution I ended up settling on.

defretry_on_error(func,wait=0.1,retry=0,*args,**kwargs):i=0whileTrue:try:returnfunc(*args,**kwargs)breakexcepttelegram.error.NetworkError:logging.exception(f"Network Error. Retrying...{i}")i+=1time.sleep(wait)ifretry!=0andi==retry:break

Which can then be used as follows:

retry_on_error(update.message.reply_photo,photo=url)

EDIT: Should bereturn func...

You must be logged in to vote
2 replies
@Poolitzer
Comment options

time.sleep() stops the whole thread, which might be good and maybe not depending on your code, and I would have probably used a for loop, but very neat solution! Thanks for sharing.

@Vihagha-Dinethma
Comment options

@Poolitzer can you share your final code please

Answer selected byPoolitzer
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
5 participants
@Happy-Ferret@Bibo-Joshi@Poolitzer@clot27@Vihagha-Dinethma

[8]ページ先頭

©2009-2025 Movatter.jp