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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:28:09+00:00 2026-06-17T11:28:09+00:00

I feel like this should be an easy solution but it’s the end of

  • 0

I feel like this should be an easy solution but it’s the end of the day and I’m brain-dead.
I am currently spawning a couple of processes, one process is receiving and storing data to a file. Another is parsing the data and third is waiting for user input to know when to stop the storing of data.

What I need to know how to do is breakout of my while loop. I’d like to not use global variables set by the parent process but if that is required I can do that.
Right now my code looks something like this:

while(packetReceived < totalToReceive):
    data, addr = sock.recvfrom(packetSize)

My thoughts were something like this:

breakout = 0
while(packetReceived < totalToReceive || breakout != 0):
    data, addr = sock.recvfrom(packetSize)

but then I need to set breakout somehow. Any help would be greatly appreciated.

  • 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-17T11:28:10+00:00Added an answer on June 17, 2026 at 11:28 am

    You can’t share state just by having a global variable in the parent process. This may appear to work, but it only works sometimes; it’s neither reliable nor predictable. Except that on Windows, it reliably and predictably never works; each child will always have its own independent copy of the flag, and therefore you will never quit.

    If you really want to do this by sharing a variable, see Sharing state between processes in the docs, but the short version is: You create a multiprocessing.Value. And then you use a multiprocessing.Condition to protect that value against races, because otherwise, there’s no guarantee that the child processes will ever see a change from the parent.

    Of course you can fake this by, e.g. creating an mmap of minimum size and just using m[0] as a flag and m.flush() instead of the condition, but that’s not really any simpler.

    The alternative way to do this is to use a multiprocessing.Pipe or similar to pass a “shut down now” message. The child processes can each spawn a thread to block on the pipe, or you can toss the pipe and your socket into a select together, or all the other usual tricks.

    There may be another, simpler option in this case: don’t use multiprocessing in the first place. Clearly your background task is not CPU-bound, since it’s just looping around reading from a socket, so why not just threading?

    Also, it strikes me that you might be able to simplify your design in other ways, which could remove this problem entirely. Do you need a file between the reading and processing jobs instead of, say, a queue, or even just a direct sequential pipeline? Can you toss the user input and the socket into the same event loop (plain old select if user input is stdin and you don’t care about Windows; use a QSocket instead of a socket.socket if user input is a Qt GUI; twisted if you’re willing to learn twisted; etc.). Or, is there real user input, or just “quit now” (or “shut down the socket and process the remaining messages now”), which you could handle with ^C?

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

Sidebar

Related Questions

I feel like this one should be easy but after numerous searches and attempts
I feel like this should be an easy thing to figure out, but I'm
I feel like this should be an easy question, but I can't get it
I feel like this should be an easy question but I can't seem to
I feel like this should be easy, but it seems to be stumping me.
I feel like this should be easy, but i am struggling to return the
I feel like this should be easy, but google is totally failing me at
I feel like this should be an easy question, but its totally bugging me.
I feel like this should be an easy task, but I am having problems.
I feel like this should be a no brainer, but clearly I'm missing something...

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.