代写代考 CS 131: Programming Languages Week 8 : Python

CS 131: Programming Languages Week 8 : Python
Khanh 1A Winter 2022

• Office Hours: Thursdays 11:00 AM – 1:00PM

Copyright By PowCoder代写 加微信 powcoder

Online, Zoom ID 5846625614 (link on bruinlearn)
• Discussion Section 1A: Fridays 10:00 AM – 11:50 AM

Course Announcement
• HW5 due: today, February 25th, 11:55pm – Cutoff time one week later
• Project due: March 7th– no submission after March. 11!
• HW6 to be released next week (details TBD) – No late submissions allowed for HW6
• Homeworks should be submitted on bruinlearn, under “Assignments”

• Project (Server application in Python) • Python and asyncio introduction

Background: Server-Client communication
• Server and client
– Definition and differences
• How do servers and clients communicate?
– From the low level: through sockets
– Many programming languages have libraries and frameworks that provides more friendly APIs
– We will practice using asyncio from Python for the project
– Protocol: the language between clients and server

• Task: Build a server herd that can synchronize data and communicate with client applications
• Each server is a separate process, to launch one of the servers
– python3 server.py Hill
– (You need to do it 5 times to launch all the serves)

Client-Server Communication: IAMAT
• Client can send their current location to any server using text-based TCP protocol
• Response from server

Server-Server communication
• After one server receiving a location, the location should be synchronized among servers
• Implement a flooding algorithm so that every server receives the message, even if it is not directly connected to the original server
– How to prevent messages from looping infinitely?
– You can decide what kind of messages server use to communicate
• If a server goes down, all other servers should still function normally – No need to propagate old messages when the server is restarted

Client-Server Communication: WHATSAT • Clients can ask what is near one of the clients
• Server uses Google Places API to find nearby locations
– Google Places API gives results in JSON format, return it to the client in the same format, just remove duplicate newlines (see project instructions for details)
• Need to create HTTP requests and send them to the Google Places API – You can use aiohttp for this

Client-Server Communication: WHATSAT • Response from server
AT Hill +0.263873386 kiwi.cs.ucla.edu +34.068930-118.445127 1520023934.918963997 {
“html_attributions” : [], “next_page_token” : “CvQ…L2E”, “results” : [
“geometry” : {
“location” : {
“lat” : 34.068921, “lng” : -118.445181
“icon” : “http://maps.gstatic.com/mapfiles/place_api/icons/university-71.png”, “id” : “4d56f16ad3d8976d49143fa4fdfffbc0a7ce8e39”,
“name” : “University of California, Los Angeles”,
“photos” : ….

• Client requests – IAMAT
– Server saves the location and propagates it among the herd
– Server calls Google Places API to check what is near the given client, and sends result back to caller.

• One message/response pair per connection
– You can close the connection after responding to a message
• Messages can contain any number of spaces/newlines between the words
– You can wait for End-of-File (EOF) by using reader.read()
• Messages can be invalid
– e.g. “WHATSAT kiwi” (missing parts)
– Respond with “? ”, e.g. “? WHATSAT kiwi”

Tips: Testing the server
• An easy way to test the server: use “telnet” or “nc”
– That’s much easier to use or control than writing a program yourselves
• Demo: issuing HTTP request with nc.

Google Places API
• https://cloud.google.com/maps-platform/places/
• Gives you information on what is around a given location
– Can also be used to find an address for given coordinates, get details on specific locations, …
• You need to create a developer account to access the API
– Free trial is enough for this project
– Do not share your key with anyone! (e.g. don’t post the key on Github)

Google Places API • We will use “Nearby Search” request API, example:
– Search places near coordiantes -33.8670522, 151.1957362 – Limits radius to 1500 meters
• Documentation
– https://developers.google.com/places/web-service/search
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522, 151.1957362&radius=1500&key=YOUR_API_KEY

Testing Google Place API requests
• You can use developer tools to try out HTTP requests easily – e.g. Postman

Making HTTP Requests in Python
• Use aiohttp library
– Only for making requests to Google Places API. Do not use it for server functionality
async with aiohttp.ClientSession() as session:
params = [(‘param-name1’, ‘some value’), (‘param-name2’, ‘100’)]
async with session.get(‘https://ucla.edu’, params=params) as resp: print(await resp.text())
• Note: you can reuse the same session for all the requests

• Max 5 pages
• Discuss pros/cons of asyncio
– Is it suitable for this kind of application?
– What problems did you run into?
– Any problems regarding type, memory management, multithreading • e.g. compare with Java
– How does asyncio compare to Node.js?
– Performance implications of asyncio?
– How easy is it to write a server using asyncio?
– How important are the asyncio features introduced in Python 3.8/3.9

• Port assignment has been posted on Piazza
– Use your assigned port on SEASnet to avoid conflicting with each other
• Make sure the requests/responses look exactly the same as instruction, as we will use fully automated tests to grade the submissions

Python & asyncio Introduction

• General-purpose, interpreted language
– Very popular, easy to develop
– Rich collection of library for neary every purpose
• We will use Python 3.9
– Python 2, although deprecated, still can be seen sometimes
• Download from: https://www.python.org
– On SEASnet: python3 (python will open Python 2)

Python resources
• https://www.learnpython.org/
– Interactive tutorial, fast and easy to get started
• https://docs.python.org/3/tutorial/ – Official tutorial
• https://docs.python.org/3/
– Reference material for the language and the official libraries

Hello World in Python
print(“Hello World”)
Hello World!

• Dynamic typing, no special syntax for declaring a new variable
x = “hello”
x = [0,1,2]
x = (0,1,2)
x = open(‘hello.py’, ‘r’)
# long integer # double float # string

Functions • Declared using def keyword:
def my_function(name):
print(“Hello, ” + name)
my_function(“Steve”)
Hello, Steve
• Note: No semi-colons, identation matters

Function Parameters
• Python allow positional and keyword parameters
def my_func(a, b=1, c=1):
print(a + b + c)
my_func(2)
my_func(2, 2, 2)
my_func(1, c=2)
# “4” # “6” # “4”

Typical program structure
• Python does not have a main function by default
– The interpreter executes code line-by-line
– The following structure can be used to emulate “main function”
def main():
print(“Hello World!”)
if __name__ == ‘__main__’: main()

Python Module
• Every file defines a module, refer to other modules with import
mymodule.py:
def print_hello(name):
print(“Hello, ” + name)
import mymodule
def main(): mymodule.print_hello(“Steve”)
if __name__ == ‘__main__’: main()

Variable Scope
• Rule: If a variable is assigned in a function, that variable is local – Unless annotated with “global” keyword.
def my_function():
my_function()
def my_function():
my_function()

Variable Scope
def my_function():
my_function()
def my_function():
print(x) x = 10
my_function()

Hello, John
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def print_greeting(self,
greeting):
print(greeting + self.name)
p = Person(“John”, 36)
p.print_greeting(“Hello, “)

• Lists are dynamic length arrays
– Fast random access, easy to add/remove elements – Uses a bit more memory
my_list = [1, 2, 3]
print(my_list[2])
print(my_list[1:])
print(my_list[0:2])
for item in my_list: print(item)

Lambda function
• Use lambda keyword to define lambda function – Suitable for simple one-liners in most cases
• Example, usage in map function
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items)) print(squared)
[1, 4, 9, 16, 25]

Lists: map/filter • Another way to express map/filter in list
my_list = [1, 2, 3, 4, 5]
new_list = [x*x for x in my_list] print(new_list)
[1, 4, 9, 16, 25]
new_list2 = [x for x in my_list if x%2 == 0] print(new_list2)

Dictionary
my_dict = { “a”: 1, “b”: “f”, 42: 3 } print(my_dict[“a”])
print(my_dict[42])
my_dict[“something new”] = 4 print(my_dict[“something new”]) 4

Multithreading vs Multiprocessing

• Difference?
Concurrency vs Parallelism

Global Interpreter Lock (GIL)
• A mutex lock that prevents multiple threads from executing Python bytecodes at once
– Exists in the default C implementation CPython
– Python memory management depends on reference counting (possible race conditions with multithreading)
– Python also uses C libraries that are not thread-safe

Consequence of GIL
• Fast single-threaded code
– Simple memory management compared to other types of GC
• Multithreading does not improve the performance of CPU intensive tasks
– In fact, might be even slower
• When can we benefit from threads in Python?

How to utilize multiple CPUs with Python?
• Multiprocessing
• Libraries
– Many numerical computation and machine learning libraries support parallel processing
– Implemened in C or other low-level language

• Single-threaded approach for concurrent programming – Suitable for I/O intensive applications
– Similar concept: node.js
• Cooperative multitasking library
– Tasks can voluntarily take breaks and let other tasks run – Compare to preemptive multitasking
• Introduced in Python 3.4 (2014)
– Relatively new, so changes often with new versions

Basic concepts
• Async keyword
– Defines that a function is a coroutine
– A function that can suspend its execution and give control to another coroutine
• Await keyword
– Suspends the execution of the current coroutine until the awaited function is finished.
async def do_something():
result = await io_operation() return result

Event loop
• Event loop runs tasks that are waiting to be executed

import asyncio
async def count(): print(“One”)
await asyncio.sleep(1) print(“Two”)
# Any IO-intensive task here
async def main():
await asyncio.gather(count(), count(), count())
if __name__ == “__main__”:
import time
s = time.perf_counter()
asyncio.run(main()) # Add to an event loop elapsed = time.perf_counter() – s
print(f”{__file__} executed in {elapsed:0.2f} seconds.”)
$ python3 countasync.py
countasync.py executed in 1.01 seconds.

import asyncio
Example: Server
async def main():
server = await asyncio.start_server(handle_connection, host=’127.0.0.1′, port=12345) await server.serve_forever()
async def handle_connection(reader, writer): data = await reader.readline()
name = data.decode()
greeting = “Hello, ” + name writer.write(greeting.encode())
await writer.drain() writer.close()
if __name__ == ‘__main__’:
asyncio.run(main()) 47

Example: Client
import asyncio
async def main():
reader, writer = await asyncio.open_connection(‘127.0.0.1’, 12345) writer.write(“John\n”.encode())
data = await reader.readline()
print(‘Received: {}’.format(data.decode()))
writer.close()
if __name__ == ‘__main__’: asyncio.run(main())

Asyncio resources • Async IO in Python: A Complete Walkthrough
– https://realpython.com/async-io-python/
• How the heck does async/await work in Python 3.5?
– https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/ • Asyncio Documentation
– https://asyncio.readthedocs.io/en/latest/

Questions?

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com