Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:09:57+00:00 2026-05-29T23:09:57+00:00

I have an ADT (PCB aka Process Control Block), I want to put them

  • 0

I have an ADT (PCB aka Process Control Block), I want to put them into a priority queue. How can I do it?

I already used How to put items into priority queues? to have a secondary priority to ensure the correct ordering of the queue. Here I can make the PCB comparable, but in another class, it may not make sense? In that case what might I do?

UPDATE

My code is very similar to that posted https://stackoverflow.com/a/9289760/292291

class PCB:
    ...

# in my class extending `PriorityQueue`
PriorityQueue.put(self, (priority, self.counter, pcb))

I think the problem is pcb is still not comparable here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:10:00+00:00Added an answer on May 29, 2026 at 11:10 pm

    OK just to close off this question. Heres what I did:

    Make the ADT comparable: Implement __lt__().

    def __lt__(self, other):
        selfPriority = (self.priority, self.pid)
        otherPriority = (other.priority, other.pid)
        return selfPriority < otherPriority
    

    This way, I can simply use queue.put(obj)

    I found that @larsmans is right in saying

    “if the priority and counter are always comparable and no two counters ever have the same value, then the entire triple is comparable“

    jiewmeng@JM:~$ python3.2
    Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
    [GCC 4.6.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Test:
    ...     def __init__(self, name):
    ...             self.name = name
    ... 
    >>> from queue import PriorityQueue
    >>> q = PriorityQueue()
    
    # duplicate priorities triggering unorderable error
    >>> q.put((2, Test("test1")))
    >>> q.put((1, Test("test1")))
    >>> q.put((3, Test("test1")))
    >>> q.put((3, Test("test1")))
    >>> q.put((3, Test("test2")))
    >>> while not q.empty():
    ...     print(q.get().name)
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "/usr/lib/python3.2/queue.py", line 195, in get
        item = self._get()
      File "/usr/lib/python3.2/queue.py", line 245, in _get
        return heappop(self.queue)
    TypeError: unorderable types: Test() < Test()
    
    # unique priority fields thus avoiding the problem
    >>> q = PriorityQueue()
    >>> q.put((3, Test("test1")))
    >>> q.put((5, Test("test5")))
    
    >>> while not q.empty():
    ...     print(q.get()[1].name)
    ... 
    test1
    test5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some big picture and I want to put it to the one
I have eclipse and ADT and android SDK installed and I want to test
I'm busy implementing a Graph ADT in C++. I have templates for the Edges
I have Eclipse Helios service release 2 with the ADT plugin. I need a
I have a question about implementing OnClickListeners for developing with the ADT. I'm unsure
everybody Recently I have use ADT to develop the android 2.2 program. The program
If I have a Haskell ADT such as: data Foo = A Int Double
I have to implement a set ADT for a pair of strings. The interface
I writing some ADT on C: I have two file date.c and date.h inside
I have started a new Android empty project. Where can i find the graphical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.