1

I am new to Python, and trying to learn it "on the job". And I am required to do this.

I am required to communicate with 3 servers with a raw socket connection. I can easily do that in a sequential manner. But I was wondering if there is a way I can communicate with these 3 servers at once? All 3 servers have different IP addresses.

Basically try to do the following but in 1 step:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST1, PORT1))
s.connect((HOST2, PORT1))
s.connect((HOST3, PORT1))

which is also need later s.sendall() & s.recv() to be parallelized.

eecs
  • 141
  • 2
  • 13

4 Answers4

1

If you only have one listening thread, you can use select to wait on multiple sockets and get woken when any of them return data:

https://docs.python.org/2/library/select.html

solidpixel
  • 10,688
  • 1
  • 20
  • 33
0

It's hard to prescribe a wealth of knowledge without knowing more information about your server protocol, what you are listening for and what you intend to do with it, et-c, but I can imagine, given no other additional information, a scenario where the communication is handled by a multiprocessing.Pool(3) where each member of the pool is mapped to an IP address and where all three send data into the same multiprocessing.Queue () which is being evaluated by a loop thread.

Any additional details?

Best of luck on your job!

  • 2
    Pretty sure that multiple *processes* are not needed here. A process is more heavyweight than a thread, so all else being equal, threads are preferable. – kfx Sep 01 '16 at 21:36
  • I added it because the question was tagged multiprocessing and nobody had prescribed such a solution. That said, kfx is entirely right about the overhead incurred from the spawning of processes. –  Sep 01 '16 at 21:40
0

Have a look at the asyncio module.

It requires Python 3, but allows to write single-threaded applications with multiple execution contexts - kind of cooperative multihreading, where the context is switched only when the user says so. You really get the best of thread and event-based concurrency.

kfx
  • 8,136
  • 3
  • 28
  • 52
0

This answer using threading actually worked out for me.

Community
  • 1
  • 1
eecs
  • 141
  • 2
  • 13