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

  • Home
  • SEARCH
  • 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 6756435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:31:25+00:00 2026-05-26T13:31:25+00:00

I have code like the following, class _Process(multiprocessing.Process): STOP = multiprocessing.Manager().Event() def __init__(self, queue,

  • 0

I have code like the following,

class _Process(multiprocessing.Process):

    STOP = multiprocessing.Manager().Event()

    def __init__(self, queue, process_fn):

        self._q = queue
        self._p = process_fn
        super().__init__()

    def run(self):

        while True:
            dat = self._q.get()
            if not dat is _Process.STOP:
                self._p(dat, self._q)
                self._q.task_done()
            else:
                self._q.task_done()
                break

but, I cannot compare STOP successfully. This isn’t that surprising when I’m using is since, I believe, is compares object id’s and from the docs ” … This is the address of the object in memory.” So, since I’m using multiple processes the memory address will be different. (I can’t compare it with == either though, and I’m not sure why this is).

This happens with any object I create with Manager(), but if I use a “true” singleton (True or False or None) it does work. Although that’s not an appropriate solution since any of those values may be valid in the queue.

So how can I create a variable, like the singletons, that can be compared across processes?

(N.B. I have tried using a dedicated class too, but get errors about it not being able to be pickled.)

Update: The answer does seem to be to use a class, but I was receiving the pickleing problems as I was only trying with an inner class. Moving it to module scope fixed the error and it works fine. – Thanks @Schnouki!


Here’s an example (and pointless) usage of the code, that shows the error …

def f(data, queue):
    print(data)

q = multiprocessing.JoinableQueue()

for i in range(4):
    p = _Process(q, f)
    p.daemon = True
    p.start()
    q.put(i)

q.join()

for i in range(4):
    q.put(_Process.STOP)

q.join()
  • 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-26T13:31:25+00:00Added an answer on May 26, 2026 at 1:31 pm

    This is a weird way to use an Event object… If you can’t use None or a boolean, I suggest you use a dedicated class and test the type of what you get from the queue:

    class StopProcessing(object):
        pass
    
    #...
    
    q.put(StopProcessing())
    
    #...
    
    while True:
        dat = self._q.get()
        if type(dat) is StopProcessing:
            # ...
    

    Or, of course, you could just keep using the multiprocessing.Event and test for its type. However this would probably be quite misleading for someone else reading your code; using a dedicated type seems much cleaner and Pythonic to me.

    EDIT: Ok, so apparently this doesn’t work because the new class is not picklable. So here’s another idea: what if you directly put the type inside your queue, like this:

    class StopProcessing(object):
        pass
    #...
    q.put(StopProcessing)
    #...
    while True:
        dat = self._q.get()
        if dat is StopProcessing:
            #...
    

    According to the pickle doc, “classes that are defined at the top level of a module” can be pickled.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have the following Code Snippet; class StringCalci { static def plus(Integer self,
I have code like the following in a UserControl: Protected Overrides Sub Render(ByVal writer
I have code that looks like the following, which works fine for displaying the
I have code that looks like the following: //unrelated code snipped resolver.reset(new tcp::resolver(iosvc)); tcp::resolver::query
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have a simple DOM code like the following <div> <div> </div> </div> I
I'm using SDL with FASM, and have code that's minimally like the following: format
I have code similar to the following with a URL like this... If I
Suppose I have a line of code that starts like the following: Func1(Func2(Func3 Is
I have code, similar to the following, that I would like to modify: Sub

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.