Browse Source

Extension for some notes/examples.

master
sıx 5 years ago committed by GitHub
parent
commit
dc30ff2c11
  1. 17
      16x_thread_2_func.py
  2. 26
      17x_threading_queue_non-class.py
  3. 29
      27x_socket_server_http.py

17
16x_thread_2_func.py

@ -0,0 +1,17 @@ @@ -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()

26
17x_threading_queue_non-class.py

@ -0,0 +1,26 @@ @@ -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."

29
27x_socket_server_http.py

@ -0,0 +1,29 @@ @@ -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
<html>
<head>
<title>Latest news from the past 24 hours</title>
</head>
<body>Wooooo</body>
</html>
""")
print "Client left."
serverAddr = ("127.0.0.1", 8080)
server = SocketServer.TCPServer(serverAddr, EchoHandler)
server.serve_forever()
Loading…
Cancel
Save