From dc30ff2c11c876bf7b7334c51170c61341139b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?s=C4=B1x?= <51x@users.noreply.github.com> Date: Thu, 12 Oct 2017 21:16:02 +0200 Subject: [PATCH] Extension for some notes/examples. --- 16x_thread_2_func.py | 17 +++++++++++++++++ 17x_threading_queue_non-class.py | 26 ++++++++++++++++++++++++++ 27x_socket_server_http.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 16x_thread_2_func.py create mode 100644 17x_threading_queue_non-class.py create mode 100644 27x_socket_server_http.py diff --git a/16x_thread_2_func.py b/16x_thread_2_func.py new file mode 100644 index 0000000..147deca --- /dev/null +++ b/16x_thread_2_func.py @@ -0,0 +1,17 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import threading +from threading import Thread +import time + +def func1(): + print 'Function 1' + time.sleep(2) +def func2(): + print 'Function 2' + time.sleep(2) + +if __name__ == '__main__': + Thread(target = func1).start() + Thread(target = func2).start() diff --git a/17x_threading_queue_non-class.py b/17x_threading_queue_non-class.py new file mode 100644 index 0000000..0f0151c --- /dev/null +++ b/17x_threading_queue_non-class.py @@ -0,0 +1,26 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# Thanks: https://www.troyfawkes.com/learn-python-multithreading-queues-basics/ + +from Queue import Queue +from threading import Thread + +def do_work(q): + while True: + print q.get() + q.task_done() + +q = Queue(maxsize=0) +num_threads = 5 + +for i in range(num_threads): + worker = Thread(target=do_work, args=(q,)) + worker.setDaemon(True) + worker.start() + +for a in range (10): + for b in range(50): + q.put(a+b*50) + q.join() + print "Work " + str(y) + ": done." diff --git a/27x_socket_server_http.py b/27x_socket_server_http.py new file mode 100644 index 0000000..4ef5664 --- /dev/null +++ b/27x_socket_server_http.py @@ -0,0 +1,29 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import SocketServer + +class EchoHandler(SocketServer.BaseRequestHandler) : + def handle(self) : + print "Connection from: ", self.client_address + data = 'dummy' + + while len(data) : + data = self.request.recv(1024) + self.request.send("""HTTP/1.0 200 OK +Content-Type: text/html + + + +Latest news from the past 24 hours + +Wooooo + +""") + print "Client left." + +serverAddr = ("127.0.0.1", 8080) + +server = SocketServer.TCPServer(serverAddr, EchoHandler) + +server.serve_forever()