0

I need to create jobs queue with certain time frame and priority, like

name      time                              priority
-----------------------------------------------------
Job 1:    from 13:00 till 14:00 5.5.2018    10
Job 2:    from 14:00 till 15:00 5.5.2018    10
Job 3:    from 14:00 till 15:00 5.5.2018    1

Now what I need is to allow Job 3 to override Job 2 because it has greater priority and to take its place. I managed to create job queue with priority using this https://pymotw.com/2/Queue/

But I need to have time frames. The program must give me info that Job 2 is delayed or canceled, in case Job 3 takes its place.

The execution result should be

Job 1 (because it has closest time)
Job 3 (because it has higher priority than Job 2)
Job 2 (would not execute, because Job 3 took its place)

1 Answer 1

1

Just ensure that the items you push in your priority queue can order naturally according to your criteria (e.g.: first datetime, then priority).

Inserting tuples in the form (datetime, priority, jobname) would do the job:

import heapq
import dateutil
import dateutil.parser
h = []

# push items into queue:
heapq.heappush(h, (dateutil.parser.parse('13:00-14:00 5.5.2018'), 10, 'Job 1'))
heapq.heappush(h, (dateutil.parser.parse('14:00-15:00 5.5.2018'), 10, 'Job 2'))
heapq.heappush(h, (dateutil.parser.parse('14:00-15:00 5.5.2018'), 1, 'Job 3'))

# extract items from queue:
print(heapq.heappop(h))
# (datetime.datetime(2018, 5, 5, 13, 0, tzinfo=tzoffset(None, -50400)), 10, 'Job 1')
print(heapq.heappop(h))
# (datetime.datetime(2018, 5, 5, 14, 0, tzinfo=tzoffset(None, -54000)), 1, 'Job 3')
print(heapq.heappop(h))
# (datetime.datetime(2018, 5, 5, 14, 0, tzinfo=tzoffset(None, -54000)), 10, 'Job 2')
Sign up to request clarification or add additional context in comments.

8 Comments

I get the error NameError: name 'datetime' is not defined. Also I need the timeframe, from - to. OK, maybe I could use starting time and duration.
@emir: where'd you get that datetime from? Not from my code I suppose.
Now it is commented, and there is no error now, but when I run it nothing happens.
@emir: the dateutil.parser would pick the start date. If you have a different criteria for establishing the priority of such datetime range, you need to transform it to some orderable data type.
@emir: try to print(heapq.heappop(...)) each of the last 3 lines
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.