-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
non blocking event loop iterUpdates #172
Conversation
What's the issue with processing updates immediately if they're available? If there's a strong need, it's still possible to execute something in the microtask queue without waiting for the next macrotask, e.g. by using let i = 0
queueMicrotask(function f () {
console.log('queueMicrotask')
if (++i > 1000) return
queueMicrotask(f)
}); And since you can control how const sleep = delay => new Promise(resolve => setTimeout(resolve, delay));
(async () => {
for await (const number of range) {
console.log(number);
await sleep(0);
}
})(); But perhaps this chain of the update microtasks can indeed get quite long and block everything else for a long time. If that is the case, I think the possible solution might be to postpone update processing until the next macrotask after a certain number of microtasks. E.g., process 100 updates in the microtask queue (counting them), and then add the next update to the macrotask queue (probably using Are you sure the blocking is because of the edit: By the way, you really should use |
I have such a case. There is an application in which you can run n tdl clients. iterUpdate can be launched in each client and there is a chance that it will happen at the same time. iterUpdate is used to track the delivery of the message and pin it to the group. |
What do you run inside the edit: For the record, |
I'm sending a message to the group. Using iterUpdat, in the for await section, I catch a hook with a successful or unsuccessful sending by the message ID. And I make a request to pin this message in the group. |
Do you have the same issue if you let the |
I figured out why there were delays. Enabled verbosityLevel: 2. I saw errors: Thank you for your time. Could you tell me what these errors might be related to, given that I don't make any requests? Is this due to the fact that there are more than 300 groups in the account and a lot of messages are coming to each one? |
300 groups should be fine. Perhaps that is caused by too frequent connects. IIRC I've received something similar if I created too many clients in a short amount of time. |
Your implementation is blocking the event loop. For example, if you run several clients in the same process and use iterUpdates in one of them, then the second client will not be able to perform any operations.
I have prepared code examples to explain what I mean.
Blocking
Non-blocking
My solution allows you to give a tick after scrolling the iterator. I also use one common promise, which saves resources and increases productivity.