Why sock.bind is always report 10048 error when in a script with multiprocessing

Discussion in 'Python' started by Junfeng Hu, Nov 18, 2011.

  1. Junfeng Hu

    Junfeng Hu Guest

    Hi All, I'm trying to leverage my core i5 to send more UDP packets with multiprocssing, but I found a interesting thing is that the socket.bind is always reporting 10048 error even the process didn't do anything about the socket.
    Here is the script

    import threading,socket,random,pp,os
    import time
    from multiprocessing import Process
    import multiprocessing.reduction

    localIP='10.80.2.24'
    localPort=2924
    remoteIP='10.80.5.143'
    remotePort=2924
    sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    #sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((localIP,localPort))
    sock.connect((remoteIP,remotePort))

    addRequest="MEGACO/1 ["+localIP+"]:"+str(localPort)+"\r\nTRANSACTION = 100 {\r\n" \
    "\tCONTEXT = $ {\r\n" \
    "\t\tADD = TDMs15c1f1/11{ \r\n" \
    " Media { LocalControl { Mode=SendReceive,tdmc/ec=on }} " \
    "\t}\r\n}}\r\n"

    def sendAddRequest(sock,addRequst):
    #for i in range(2500):
    #sock.send(addRequest)
    print "hello"



    if __name__ == '__main__':
    reader = Process(target=sendAddRequest,args=(sock,addRequest))
    reader.start()


    Here is the output

    D:\Python test>mythread2.py
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "C:\Python27\lib\multiprocessing\forking.py", line 346, in main
    prepare(preparation_data)
    File "C:\Python27\lib\multiprocessing\forking.py", line 461, in prepare
    '__parents_main__', file, path_name, etc
    File "D:\Python test\mythread2.py", line 12, in <module>
    sock.bind((localIP,localPort))
    File "C:\Python27\lib\socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
    socket.error: [Errno 10048] Only one usage of each socket address (protocol/netw
    ork address/port) is normally permitted
     
    Junfeng Hu, Nov 18, 2011
    #1
    1. Advertising

  2. Junfeng Hu

    Junfeng Hu Guest

    I did a test on linux, it works well, so the issue is related to os.
     
    Junfeng Hu, Nov 18, 2011
    #2
    1. Advertising

  3. Re: Why sock.bind is always report 10048 error when in a script withmultiprocessing

    On Fri, Nov 18, 2011 at 9:23 PM, Junfeng Hu <> wrote:
    > Hi All, I'm trying to leverage my core i5 to send more UDP packets with multiprocssing, but I found a interesting thing is that the socket.bind is always reporting 10048 error even the process didn't do anything about the socket.
    > sock.bind((localIP,localPort))
    > socket.error: [Errno 10048] Only one usage of each socket address (protocol/netw
    > ork address/port) is normally permitted


    Try setting the socket to SO_REUSEADDR.

    ChrisA
     
    Chris Angelico, Nov 18, 2011
    #3
  4. Junfeng Hu

    Junfeng Hu Guest

    Re: Why sock.bind is always report 10048 error when in a script withmultiprocessing

    Thanks
    Yes, I had tried this before, so you could find that I comment the line
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    Here is the results.
    D:\Python test>mythread2.py
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "C:\Python27\lib\multiprocessing\forking.py", line 347, in main
    self = load(from_parent)
    File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
    File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
    File "C:\Python27\lib\pickle.py", line 1133, in load_reduce
    value = func(*args)
    File "C:\Python27\lib\multiprocessing\reduction.py", line 167, in rebuild_sock
    et
    _sock = fromfd(fd, family, type_, proto)
    File "C:\Python27\lib\multiprocessing\reduction.py", line 156, in fromfd
    s = socket.fromfd(fd, family, type_, proto)
    AttributeError: 'module' object has no attribute 'fromfd'
     
    Junfeng Hu, Nov 18, 2011
    #4
  5. Junfeng Hu

    Junfeng Hu Guest

    Thanks
    Yes, I had tried this before, so you could find that I comment the line
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    Here is the results.
    D:\Python test>mythread2.py
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "C:\Python27\lib\multiprocessing\forking.py", line 347, in main
    self = load(from_parent)
    File "C:\Python27\lib\pickle.py", line 1378, in load
    return Unpickler(file).load()
    File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
    File "C:\Python27\lib\pickle.py", line 1133, in load_reduce
    value = func(*args)
    File "C:\Python27\lib\multiprocessing\reduction.py", line 167, in rebuild_sock
    et
    _sock = fromfd(fd, family, type_, proto)
    File "C:\Python27\lib\multiprocessing\reduction.py", line 156, in fromfd
    s = socket.fromfd(fd, family, type_, proto)
    AttributeError: 'module' object has no attribute 'fromfd'
     
    Junfeng Hu, Nov 18, 2011
    #5
  6. Junfeng Hu

    Junfeng Hu Guest

    Re: Why sock.bind is always report 10048 error when in a script withmultiprocessing

    And actually ,the socket hadn't been used in this script.
     
    Junfeng Hu, Nov 18, 2011
    #6
  7. Junfeng Hu

    Junfeng Hu Guest

    And actually ,the socket hadn't been used in this script.
     
    Junfeng Hu, Nov 18, 2011
    #7
  8. Re: Why sock.bind is always report 10048 error when in a script withmultiprocessing

    On Sat, Nov 19, 2011 at 2:51 AM, Junfeng Hu <> wrote:
    > And actually ,the socket hadn't been used in this script.


    Doesn't matter that you haven't used it; you're binding to the port,
    that's what causes the 10048.

    I think the main problem is that you're trying to share sockets across
    processes, but I haven't used the Python multiprocessing module with
    sockets before. I would recommend, if you can, creating separate
    sockets in each subprocess; that might make things a bit easier.

    ChrisA
     
    Chris Angelico, Nov 18, 2011
    #8
  9. Junfeng Hu

    Junfeng Hu Guest

    Re: Why sock.bind is always report 10048 error when in a script withmultiprocessing

    Hi Chris.
    The socket only binded once. That's the problem I'm puzzleing, I think it may a bug of multiprocessing in windows, or something I missed.
     
    Junfeng Hu, Nov 18, 2011
    #9
  10. Junfeng Hu

    Junfeng Hu Guest

    Hi Chris.
    The socket only binded once. That's the problem I'm puzzleing, I think it may a bug of multiprocessing in windows, or something I missed.
     
    Junfeng Hu, Nov 18, 2011
    #10
  11. Re: Why sock.bind is always report 10048 error when in a script withmultiprocessing

    On Sat, Nov 19, 2011 at 3:11 AM, Junfeng Hu <> wrote:
    > Hi Chris.
    > The socket only binded once. That's the problem I'm puzzleing, I think it may a bug of multiprocessing in windows, or something I missed.


    I don't know how multiprocessing goes about initializing those
    subprocesses; I suspect that's where it creates the additional
    sockets.

    ChrisA
     
    Chris Angelico, Nov 18, 2011
    #11
  12. Junfeng Hu

    MRAB Guest

    Re: Why sock.bind is always report 10048 error when in a script withmultiprocessing

    On 18/11/2011 15:48, Junfeng Hu wrote:
    > Thanks
    > Yes, I had tried this before, so you could find that I comment the line
    > sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    > Here is the results.
    > D:\Python test>mythread2.py
    > Traceback (most recent call last):
    > File "<string>", line 1, in<module>
    > File "C:\Python27\lib\multiprocessing\forking.py", line 347, in main
    > self = load(from_parent)
    > File "C:\Python27\lib\pickle.py", line 1378, in load
    > return Unpickler(file).load()
    > File "C:\Python27\lib\pickle.py", line 858, in load
    > dispatch[key](self)
    > File "C:\Python27\lib\pickle.py", line 1133, in load_reduce
    > value = func(*args)
    > File "C:\Python27\lib\multiprocessing\reduction.py", line 167, in rebuild_sock
    > et
    > _sock = fromfd(fd, family, type_, proto)
    > File "C:\Python27\lib\multiprocessing\reduction.py", line 156, in fromfd
    > s = socket.fromfd(fd, family, type_, proto)
    > AttributeError: 'module' object has no attribute 'fromfd'


    The documentation for socket.fromfd says:

    Availability: Unix.

    You're using Microsoft Windows.
     
    MRAB, Nov 18, 2011
    #12
  13. Junfeng Hu

    Junfeng Hu Guest

    On Nov 18, 10:55 am, MRAB <> wrote:
    > On 18/11/2011 15:48, Junfeng Hu wrote:
    >
    >
    >
    >
    >
    > > Thanks
    > > Yes, I had tried this before, so you could find that I comment the line
    > > sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    > > Here is the results.
    > > D:\Python test>mythread2.py
    > > Traceback (most recent call last):
    > >    File "<string>", line 1, in<module>
    > >    File "C:\Python27\lib\multiprocessing\forking.py", line 347, in main
    > >      self = load(from_parent)
    > >    File "C:\Python27\lib\pickle.py", line 1378, in load
    > >      return Unpickler(file).load()
    > >    File "C:\Python27\lib\pickle.py", line 858, in load
    > >      dispatch[key](self)
    > >    File "C:\Python27\lib\pickle.py", line 1133, in load_reduce
    > >      value = func(*args)
    > >    File "C:\Python27\lib\multiprocessing\reduction.py", line 167, in rebuild_sock
    > > et
    > >      _sock = fromfd(fd, family, type_, proto)
    > >    File "C:\Python27\lib\multiprocessing\reduction.py", line 156, in fromfd
    > >      s = socket.fromfd(fd, family, type_, proto)
    > > AttributeError: 'module' object has no attribute 'fromfd'

    >
    > The documentation for socket.fromfd says:
    >
    >      Availability: Unix.
    >
    > You're using Microsoft Windows.- Hide quoted text -
    >
    > - Show quoted text -


    Yes, but my question is , how to make such script work in windows.
     
    Junfeng Hu, Nov 19, 2011
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johannes Eble
    Replies:
    1
    Views:
    1,310
    Grant Edwards
    Jul 16, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,031
    Smokey Grindel
    Dec 2, 2006
  3. Nikolaus Rath
    Replies:
    1
    Views:
    78
    Roy Smith
    Feb 17, 2014
  4. Chris Angelico
    Replies:
    0
    Views:
    77
    Chris Angelico
    Feb 17, 2014
  5. Nikolaus Rath
    Replies:
    0
    Views:
    85
    Nikolaus Rath
    Feb 18, 2014
Loading...

Share This Page