Separate _tasks_inserted set from insertion

Move most of handle_tasks() into _handle_tasks() to allow
AutoContext to insert some tasks before fully exiting.

Also add a parameter to allow different batch_sizes.