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

J

Junfeng Hu

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
 
C

Chris Angelico

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
 
J

Junfeng Hu

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'
 
J

Junfeng Hu

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'
 
C

Chris Angelico

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
 
J

Junfeng Hu

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.
 
J

Junfeng Hu

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.
 
C

Chris Angelico

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
 
M

MRAB

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.
 
J

Junfeng Hu

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top