[Python-Dev] Re: Should set objects maintain insertion order too?

2019年12月27日 21:05:55 -0800

> On Dec 27, 2019, at 19:48, Tim Peters <[email protected]> wrote:
> 
> So, ya, I've seen and implemented lots of work queues along these
> lines - but an OrderedSet would be an "attractive nuisance"
> (offhandedly appearing to solve a problem it doesn't actually
> address):
> 
> jobs = some_kind_of_queue()
> finished_jobs = set()
> ...
> while jobs:
> job = jobs.get()
> if job in finished_jobs:
> continue
> try:
> work on the job
> possibly adding (sub)jobs to the `jobs` queue
> except TransientExceptions:
> jobs.put(job) # try again later
> else:
> finished_jobs.add(job)
Well, if an OrderedSet were designed to gracefully handle resizes during 
iteration, something like this may make sense:
jobs = OrderedSet(initial_jobs)
for job in jobs:
 new_jobs = process(job)
 jobs |= new_jobs
... # jobs is now a set of every job processed
A dictionary with None values comes close if you replace the union line with a 
jobs.update(new_jobs) call (and ignore resizing issues), but it breaks because 
repeated jobs are shuffled to the end of the sequence and would be processed 
again.
Brandt
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/SXGD5G5EY4YMXDG42AU7OSNVCUUU25DI/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to