Reputation: 969
I am developing a Python application to transfer files using TCP and is new to Python language. I have already coded out the client side code and server side code and it is working if I run them individually. Unfortunately, I needed to put them all together in a same file, ie. main.py, that will run the server side code and client side code. For example, every network node should act as a server and client at the same time. Thus, I will need it to constantly listen for new requests from other nodes (so need to run the server side code) and at the same time send new requests to other nodes that acts as server (so in this case, it is acting as a client). I have thought of using threading but kept getting stuck at the server side code's thread.
here's an example of my main.py code that deals with the threading.
if __name__ == '__main__':
serverThread_stop = Event()
serverThread = Thread(target = TCPServer, args = (1111,'test.txt'))
serverThread.start()
serverThread.join()
clientThread_stop = Event()
clientThread = Thread(target = TCPClient, args = ('127.0.0.1',1111,'test.txt'))
clientThread.start()
clientThread.join()
clientThread_stop.set()
serverThread_stop.set()
As I'm quite new to Python and socket programming so I might have made some mistakes with the threading code. Can someone help me out with this problem? Thanks in advance!
Upvotes: 1
Views: 196
Reputation: 9879
You immediately call serverThread.join()
before starting the second Thread.
The problem is that join blocks until the thread terminates.
I'd either move or completely remove the call to join
.
Upvotes: 1