Commits

Sébastien Martinez committed fa95937

Adding an example for dynamic thread adding

Comments (0)

Files changed (4)

examples/threads/dynamic-threads/dynamic_threads.py

+#!/usr/bin/env pypy
+
+import pymoult
+import pymoult.controllers
+import socket
+import time
+
+
+def echo(thread):
+	print("This is thread "+str(thread)+" in 1st version")
+
+
+def genMain(num):
+	def main():
+		while True:
+			echo(num)
+			time.sleep(2)
+	return main
+
+threads = pymoult.controllers.start_active_threads(None,genMain(1),genMain(2))
+pymoult.controllers.register_threads(threads)
+
+
+
+
+
+

examples/threads/dynamic-threads/dynamic_threads_example.sh

+#!/bin/bash
+
+hostname=`hostname`
+
+echo "Starting Dynamic-Thread example"
+./dynamic_threads.py &
+prog=$?
+sleep 10
+echo "applying update 1"
+echo "update update1.py" | netcat $hostname 4242
+sleep 10
+echo "applying update 2"
+echo "update update2.py" | netcat $hostname 4242
+sleep 10
+kill $prog
+
+

examples/threads/dynamic-threads/update1.py

+#parsed
+
+import sys
+import pymoult.controllers
+import pymoult.stack.tools
+
+def new_echo(thread):
+	print("This is thread "+str(thread)+" in 2d version") 
+
+myupdate = pymoult.stack.tools.safe_redefine("echo",new_echo,"__main__")
+
+threads = sys.modules["__main__"].threads
+pymoult.controllers.set_update_function(myupdate,threads[0])
+
+newthreads = pymoult.controllers.start_active_threads(None,sys.modules["__main__"].genMain(3))
+pymoult.controllers.register_threads(newthreads)
+threads+=newthreads

examples/threads/dynamic-threads/update2.py

+#parsed
+
+import sys
+import pymoult.controllers
+import pymoult.stack.tools
+
+def new_echo2(thread):
+	print("This is thread "+str(thread)+" in 3rd version") 
+
+
+myupdate = pymoult.stack.tools.safe_redefine("echo",new_echo2,"__main__")
+
+threads = sys.modules["__main__"].threads
+pymoult.controllers.set_update_function(myupdate,threads[2])
+