Yeah, you have to get creative. :-) What needs to happen to the 3rd party code on cancel? Does the callback just have to raise an exception?
You probably have to come up with some scheme where the callback just checks a flag and if the flag is set raises an exception. Is there any reason why your 3rd party code couldn't run in a thread (pool)? Assuming a threadpool is okay, you could use run_in_executor()<https://docs.python.org/3/library/asyncio-eventloop.html?highlight=run_in_executor#asyncio.BaseEventLoop.run_in_executor>to run ThirdPartyJob. When you receive a cancel message you just set the flag that the callback checks. You also have to consider whether it's okay to be running multiple 3rd party jobs concurrently, and how to keep track of them separately for the purpose of cancellation. If not, you have to keep track of whether one is already running. On Thu, May 22, 2014 at 2:55 AM, Phil Schaf <[email protected]>wrote: > Am Donnerstag, 22. Mai 2014 03:05:43 UTC+2 schrieb Guido van Rossum: > >> Ah, I think I understand. You can probably solve this by having a Future >> and making the callback set the Future's result. Perhaps you should also >> run the 3rd party code in a thread. >> > > Thanks for your help, but I think I didn’t understand: > > The callback gets called a few hundreds of times. I want to give the user > a chance to cancel at any time, a good approximation being “every time the > callback gets called”. > > One future would only work if the callback would be called one time, no? > -- --Guido van Rossum (python.org/~guido)
