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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:10:03+00:00 2026-05-24T19:10:03+00:00

These code is the Server part of my proxy program, and its function is

  • 0

These code is the Server part of my proxy program, and its function is creating the socket and fork four process to accept one by one.

In my program I use gevent model to dispatch all my function and before I alter it to multiple process, my program is all right. but now when I use the second process, the first one stop running, I don’t find where is wrong, maybe the ‘accept’ function or my event is stop dispatch.

It have already bothered me for two days I hope someone can help me.

BTW, my English is poor, I try my best to explain it, hoping you can understand.

 class Client(object):
    def __init__(self, ent, ev):
        ...  

    def receive( self ):
        ...
        if "Content-Length" in dic:
            self.ent_s_send = core.event(core.EV_WRITE,
                                         self.conn.fileno(),
                                         self.ser_send,
                                         [self.conn,self.body]
                                         )
            self.recv_ent = core.event(core.EV_READ, 
                                       self.sock.fileno(),
                                       self.recv_content
                                      )
            self.recv_ent.add()
        ...

    def recv_content(self, ent, ev):
        ...
        self.n = self.sock.recv_into(self.msg,
                                     min(self.total-self.num, 20000),
                                     socket.MSG_DONTWAIT)

        **time.sleep(0.1)**  
        #if i add it here to let the event slow down the problem solved, how it could be? 

        self.num += self.n
        self.msg_buffer.fromstring(self.msg.tostring()[:self.n])
        ...
        if self.total > self.num:  #if not the last msg continue recving and sending...
            self.ent_s_send.add()
            self.recv_ent.add()
        ...

    def ser_send(self, ent, ev):
        ...
        num = self.conn.send(self.msg_buffer,socket.MSG_DONTWAIT)
        ...
        self.msg_buffer = self.msg_buffer[num:]

 ...
 ...

 class Server(object):
    def __init__( self ):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind(('localhost', 18001))
        self.sock.listen(50)
        self.mutex = multiprocessing.Lock()

    def loop( self, ):

        for i in range(0,4):
            pid = os.fork()
            if pid == 0 or pid == -1:
                break

        if pid == -1:
            print "Fork failed!!!"
            sys.exit()

        elif pid == 0:   **# create four child ps to accept the socket!**
            print "Child  PID =  %d" % os.getpid()
            core.init()
            self.event = core.event(core.EV_READ,
                                self.sock.fileno(),
                                self.onlink)
            self.event.add()
            core.dispatch()

        else:
            os.wait()

    def onlink( self, ent, ev):
        self.mutex.acquire()
        print 'Accept PID = %s' % os.getpid()
        try:
            self.conn, self.addr = self.sock.accept()   
            **#I think 'accept' is the the problem, but I cannot see how.** 

        except socket.error, why:
            if why.args[0] in [ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED]:
                return
            else:
                raise
        print self.sock,self.conn,self.addr
        self.mutex.release()
        print 'Release PID = %s' % os.getpid()
        cc = Chat( self.conn, self.sock )
        self.event.add()



if __name__ == '__main__':

    s1 = Server()
    s1.loop()
  • 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-24T19:10:04+00:00Added an answer on May 24, 2026 at 7:10 pm

    accept() is a blocking call. It’ll wait indefinitely for a client to connect. Holding a mutex over a blocking operation like that is a Bad IdeaTM since you totally lock all other concurrent processes out.

    Also, as @Maxim noted in the comments, you don’t really need to lock around accept(). Just let the OS arbitrate dequeuing of incoming connections and dispatch them to your processes.

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

Sidebar

Related Questions

I have a server side code to process uploaded binary files: class UploadHandler(webapp.RequestHandler): def
The code segment is from a page called sites-catchup.html. There is a simple server
I normally in my projects use such these code: If user.IsInRole(Admin) Then deleteButton.Visible =
I am trying to test some of these code here http://ha.ckers.org/xss.html on my code.
In my models.py, I user these code to extent two fields: User.add_to_class('bio', models.TextField(blank=True)) User.add_to_class('about',
text = urllib.urlopen('www.text.com').read() frase = re.search(your text here(.*), text).group() With these code, I get
I am working on some source code these days. In some codes, I find
Which of these pieces of code is faster? if (obj is ClassA) {} if
So I have this code for these Constructors of the Weapon class: Weapon(const WeaponsDB
How do you add code to these events for native c++? I couldn't find

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.