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 8330785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:13:13+00:00 2026-06-09T02:13:13+00:00

I have a small piece of code, written in python which has a Generate

  • 0

I have a small piece of code, written in python which has a Generate class which returns the next number when ever called.

class Generate:
    def iter(self):
        self.next1 = 1
        return self
    def next(self):
        self.curr = self.next1
        self.next1 += 1
        return self.curr

def generateNos():
    for i in xrange(3):
        print g.next()

g = Generate().iter()

generateNos()
generateNos()

And a function generateNos, which generate 3 Nos only. So when i call this function two times as below, i get output as:-
1
2
3
4
5
6

Now, When I spawn two processes for calling this generateNos(), I get the output as:- 1
2
3
1
2
3

The code for the same is:-

from multiprocessing import Process, Manager
class Generate:
    def iter(self):
        self.next1 = 1
        return self
    def next(self):
        self.curr = self.next1
        self.next1 += 1
        return self.curr

def generateNos():
    for i in xrange(3):
        print g.next()

g = Generate().iter()

p1 = Process(target = generateNos)
p2 = Process(target = generateNos)

p1.start()
p1.join()

p2.start()
p2.join()

Now it seems this Generate class get re-initialized again in child process, which I don’t want. How can i avoid this?

Wanted the same behavior as, calling generateNos twice one after another.

Any hack, so that Generate() object ” g = Generate().iter()” doesn’t gets called again in child proesses, It should be called only once in Parent Process.

  • 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-06-09T02:13:15+00:00Added an answer on June 9, 2026 at 2:13 am

    You have to have the object shared between processes via shared memory or some other mechanism that allows them to actually share the same object instance. Starting a new subprocess actually pickles everything you pass in to the child process then rebuilds a copy in the subprocess.

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

Sidebar

Related Questions

I have written a small piece of code which handles the input of a
What I have written a small piece of code to find and remove if
I have just written a small piece of code and it struck me that
I have written some code in which a small part of the code takes
I have written a small piece of code to calculate quadratic equations, but if
I have a small piece of code here for your consideration which puzzles me
i have written a small piece of code. This code first blocks the {SIGSEGV},
I have written a small piece of code regarding the dynamic loading of assemblies
I have a small piece of c code which should run an awk command
I've written a small piece of code which opens up an Excel document on

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.