Marc-Andre Lemburg added the comment: On 30.11.2012 22:27, Serhiy Storchaka wrote: > > Serhiy Storchaka added the comment: > >> try: >> mapping = {} >> mapping.max_collisions = 100 >> mapping.update(source) >> except CollisionLimitError: >> return 'no thank you' > > May be use a more general solution? > > try: > with run_with_timeout(timeout=100, timer=collisions_count): > mapping = insert_untrusted_data(source) > except TimeoutError: > return 'no thank you' > > (You can can use different measurement for timeout: user time, real time, > ticks > count, collisions count, or even a user defined timer).
Sure, as long as there's a way to break into the execution, any such method would do. The problem is that you'd have to check for the timeout at some point and the .update() call is running completely in C, so the only way to break into execution is either by explicitly adding a runtime check to the code, or use a signal (which is a can of worms better avoided :-)). Collision counting is one such method of detecting that something is likely not working according to plan, but it's really only another way of implementing the explicit runtime check. Using other counters or timers would work just as well. As long as you know that there are places in your code that can produce CPU time overloads, you can address those issues. The dictionary implementation is one such place, where you can run into the problem, but there are usually many other such areas in more complex applications as well, e.g. calculations that enter endless loops for some parameters, deadlocks, I/O operations that take unusually long (e.g. due to disk errors), poorly written drivers of all sorts, etc. etc. If there's a way to solve all these things in general and without explicit runtime checks, I'd like to know :-) ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue14621> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com