forked from lrlrlrlr/COMP9331_COMP3331_20T3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
80 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
Binary file not shown.
Binary file not shown.
This file contains 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
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()) | ||
|
||
|
This file contains 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
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.') |
This file contains 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
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) |
This file contains 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
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 not shown.