Skip to content

Commit

Permalink
w5 update
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlrlrlr committed Oct 18, 2020
1 parent e27370c commit 60d7078
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 51 deletions.
6 changes: 6 additions & 0 deletions assignment/UDPServer3.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ def recv_handler():
message = message.decode()
#get lock as we might me accessing some shared data structures
with t_lock:
login_status = False
currtime = dt.datetime.now()
date_time = currtime.strftime("%d/%m/%Y, %H:%M:%S")
print('Received request from', clientAddress[0], 'listening at', clientAddress[1], ':', message, 'at time ', date_time)

# while not login_status:
# login_status = login(clientAddress)

if(message == 'Subscribe'):
#store client information (IP and Port No) in list
clients.append(clientAddress)
Expand All @@ -55,6 +60,7 @@ def recv_handler():
t_lock.notify()



def send_handler():
global t_lock
global clients
Expand Down
Binary file not shown.
Binary file modified assignment/assignment_V1.1.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions assignment/client_tut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from socket import *


clientSocket = socket(AF_INET, SOCK_DGRAM)
while True:
message = input("input your command: ")
clientSocket.sendto(message.encode(),('127.0.0.1', 12000))
msg = clientSocket.recv(1024)
if msg:
print(msg.decode())


30 changes: 30 additions & 0 deletions assignment/multiproc_tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import logging
import os, time, random
import threading

logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-9s) %(message)s', )


def long_time_task(name):
logging.debug('Run task %s (%s)...' % (name, os.getpid()))
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
logging.debug('Task %s runs %0.2f seconds.' % (name, (end - start)))


if __name__ == '__main__':
logging.debug('Parent process %s.' % os.getpid())

ts = list()
for i in range(5):
ts.append(threading.Thread(target=long_time_task, args=(i,)))

for t in ts:
t.start()

logging.debug('Waiting for all subprocesses done...')
for t in ts:
t.join()

logging.debug('All subprocesses done.')
14 changes: 14 additions & 0 deletions assignment/server_tut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from socket import *

serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('127.0.0.1', 12000))

while True:
msg,client_addr = serverSocket.recvfrom(1024)
if msg:
print(msg.decode())
if msg.decode() == 'login':
serverSocket.sendto("please give me your username and password!".encode(), client_addr)

else:
serverSocket.sendto("unknown command!".encode(), client_addr)
69 changes: 18 additions & 51 deletions assignment/threading_tutorial.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,24 @@
import time
import threading
import logging
from multiprocessing import Pool
import os, time, random

logging.basicConfig(level=logging.DEBUG, format='(%(threadName)-9s) %(message)s', )

# def loop():
# print(f"Thread {threading.current_thread().name} is running")
# n = 0
# while n < 5:
# n = n +1
# print(f'Thread {threading.current_thread().name} >>> {n}')
# time.sleep(1)
# print(f"Thread {threading.current_thread().name} ended")


# print(f'thread {threading.current_thread().name} is running...')


def wait(n):
time.sleep(n)
print(f"{n} complete!")


def save_money(n):
global balance

# print(balance,'+',n)
balance += n
# print(balance,'-',n)
balance -= n
# print(balance)


def run_thread(n):
for i in range(1000000):
lock.acquire()
try:
save_money(n)
except:
lock.release()
def long_time_task(name):
logging.debug('Run task %s (%s)...' % (name, os.getpid()))
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
logging.debug('Task %s runs %0.2f seconds.' % (name, (end - start)))


if __name__ == '__main__':

balance = 0
lock = threading.Lock()

t1 = threading.Thread(target=run_thread, args=(5,))
t2 = threading.Thread(target=run_thread, args=(8,))

t1.start()

t2.start()

t1.join()
t2.join()

print(balance)
logging.debug('Parent process %s.' % os.getpid())
p = Pool(4)
for i in range(5):
p.apply_async(long_time_task, args=(i,))
logging.debug('Waiting for all subprocesses done...')
p.close()
p.join()
logging.debug('All subprocesses done.')
Binary file added assignment/全学期解析班w5.pdf
Binary file not shown.

0 comments on commit 60d7078

Please sign in to comment.