Created
February 18, 2012 12:29
-
-
Save phihag/1859087 to your computer and use it in GitHub Desktop.
Thread safety demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import subprocess | |
import sys | |
from threading import Thread | |
from time import sleep | |
def executeCommand(command): | |
_subProcess = subprocess.Popen( | |
command, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
shell=True | |
) | |
stdout, stderr = _subProcess.communicate() | |
exitCode = _subProcess.returncode | |
if exitCode: | |
print("Failure executing command: %r" % command) | |
return "%s::%s" % (stdout, stderr), exitCode | |
else: | |
return stdout, exitCode | |
class MainRecvThread(Thread): | |
def __init__(self, basePort, count): | |
Thread.__init__(self) | |
self.__basePort = basePort | |
self.__count = count | |
def run(self): | |
threads = [WorkerRecvThread(self.__basePort + i) for i in range(self.__count)] | |
for t in threads: | |
t.start() | |
for t in threads: | |
t.join() | |
class WorkerRecvThread(Thread): | |
def __init__(self, port): | |
Thread.__init__(self) | |
self.__port = port | |
def run(self): | |
command = "nc -l 127.0.0.1 -p %s | cat > /tmp/tts_%s" %\ | |
( | |
self.__port, self.__port | |
) | |
output, exitCode = executeCommand(command) | |
if exitCode: | |
print("Failure with command %r: [%s]: %r" % | |
(command, exitCode, output)) | |
elif len(output) > 0: | |
print("Output: %r" % output) | |
class MainSendThread(Thread): | |
def __init__(self, sourceFile, basePort, count): | |
Thread.__init__(self) | |
self.__sourceFile = sourceFile | |
self.__basePort = basePort | |
self.__count = count | |
def run(self): | |
threads = [WorkerSendThread(self.__sourceFile, self.__basePort + i) for i in range(self.__count)] | |
for t in threads: | |
t.start() | |
for t in threads: | |
t.join() | |
class WorkerSendThread(Thread): | |
def __init__(self, sourceFile, port): | |
Thread.__init__(self) | |
self.__sourceFile = sourceFile | |
self.__port = port | |
def run(self): | |
command = "cat %s | nc --send-only 127.0.0.1 %s" %\ | |
( | |
self.__sourceFile, self.__port | |
) | |
print(command) | |
output, exitCode = executeCommand(command) | |
if exitCode: | |
print("Failure command %r: [%s]: %r" % | |
(command, exitCode, output)) | |
elif len(output) > 0: | |
print("Output: %r" % output) | |
if __name__ == "__main__": | |
sourceFile = "/bin/echo" | |
basePort = 64000 | |
count = 10 | |
mainRecvThread = MainRecvThread(basePort, count) | |
mainRecvThread.start() | |
sleep(2) | |
mainSendThread = MainSendThread(sourceFile, basePort, count) | |
mainSendThread.start() | |
mainSendThread.join() | |
mainRecvThread.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment