You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _labs/concurrent-http-server.md
+6-11Lines changed: 6 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Use the GitHub Classroom link posted in the Teams channel for the lab to accept
20
20
21
21
For this lab, you will be extending your HTTP server to handle multiple clients _at once_. The rest of your server will stay the same. There are multiple ways of supporting multiple clients. A typical approach is to spawn a new thread or process for each client that connects. That thread or process is responsible for communicating with a specific client. There is another approach that uses a system call (`poll` or `select`) to determine which sockets are ready to receive data from or write data to. Since with socket programming, most of the time your program is waiting around for the sockets to send or receive data, this allows your program to handle multiple sockets at once. Instead of creating a new thread or process for each client socket, one process keeps track of many sockets at once.
22
22
23
-
For this lab, you are required to implement a concurrent server in two ways: using threads and using a thread pool. Using standard threads, when a new request comes in, a thread is created and the socket is passed to that thread. The newly spawned thread is now responsible for receiving and sending data while the main thread is still accepting new clients. With a thread pool, you create a bunch of threads before starting your server and hand tasks (i.e., new clients) to threads that are idle. As mentioned in lecture, threads have their own set of issues, largely shared memory. To limit these issues, try to use local variables as much as possible. You should not need to use mutex/locks in this lab! For extra credit, you can also implement your sever using Python's [Asynchronous I/O](https://docs.python.org/3/library/asyncio.html) library.
23
+
For this lab, you are required to implement a concurrent server in three ways: using threads, using a thread pool, and using `select`. Using standard threads, when a new request comes in, a thread is created and the socket is passed to that thread. The newly spawned thread is now responsible for receiving and sending data while the main thread is still accepting new clients. With a thread pool, you create a bunch of threads before starting your server and hand tasks (i.e., new clients) to threads that are idle. As mentioned in lecture, threads have their own set of issues, largely shared memory. To limit these issues, try to use local variables as much as possible. You should not need to use mutex/locks in this lab! For the `select` implementation, you will need to use the [`select` call](https://docs.python.org/3/library/selectors.html) to determine which sockets are ready to read from or write to. This is a more advanced technique and requires a good understanding of how sockets work. You will need to maintain a list of all the client sockets that are connected to your server and use `select` to determine which sockets are ready for reading or writing.
24
24
25
25
As part of writing a well behaving server, you will need to appropriately handle the threads when you are exiting (the user hits `ctrl-c`). This allows your server to finishing handling clients that have already connected before shutting down the server. To do this, you must join all spawned threads.
Only `thread` and `thread-pool` are required in this lab. For extra credit, you can add the `async` option to the concurrency flag.
48
-
49
47
### Benchmarking
50
48
51
49
As part of this lab, you will be benchmarking the different approaches to concurrency. To do so, you will be using the [wrk2](https://github.com/giltene/wrk2) tool. It sends a bunch of HTTP requests to your server and measures how long it takes to respond. You will need to build `wrk2` yourself by cloning the [linked repo](https://github.com/giltene/wrk2) and running `make`. After running `make`, a `wrk` executable will be generated. I have verified that it works on the embedded lab computers. You will benchmark the performance of your lab 5 single thread server vs. threads vs processes. Record the results in `benchmark.md` of your repository. For consistency, record the results of your benchmark using this configuration:
@@ -58,7 +56,7 @@ Run the benchmark **3 or more times** and report the best run. You should turn o
58
56
59
57
## Objectives
60
58
61
-
- Learn how to use threadingand processes in Python.
59
+
- Learn how to use threading, thread pools, and select in Python.
62
60
63
61
- Learn how to make an HTTP server concurrent.
64
62
@@ -75,7 +73,7 @@ Run the benchmark **3 or more times** and report the best run. You should turn o
75
73
76
74
- You can only use the low-level [threading.Thread](https://docs.python.org/3/library/threading.html#thread-objects) objects. All high-level concurrency libraries like [concurrent.futures](https://docs.python.org/3/library/concurrent.futures.html) is not allowed. Python has implementations of thread pools, but you must implement these yourself to get the credit.
77
75
78
-
- Add the `-c`/`--concurrency` flags to your program. You only need to support `thread` and `thread-pool`. If the concurrency flag is not provided, the default value should be `thread`. If you decided not to do the extra credit, do NOT include the `async` concurrency flag.
76
+
- Add the `-c`/`--concurrency` flags to your program. If the concurrency flag is not provided, the default value should be `thread`.
79
77
80
78
- You must gracefully shutdown your server, waiting for all client sockets to finish.
81
79
@@ -85,11 +83,6 @@ Run the benchmark **3 or more times** and report the best run. You should turn o
85
83
86
84
- All other requirements are the same as lab 5.
87
85
88
-
89
-
## Extra Credit
90
-
91
-
For extra credit, you can implement and **benchmark** your server using the Async I/O library in Python. For the Async I/O, you will need to import the [asyncio](https://docs.python.org/3/library/asyncio.html) module. This will require you to reimplement your code using the `async` and `await` key words since async code and sync code can't mix. You will receive **15% extra credit** on this assignment.
92
-
93
86
## Testing
94
87
95
88
One way to test that your server is handling multiple clients is to add an artificial delay (e.g., `sleep`) to your program, just for testing purposes. This technique will simulate a request taking a long time and show if you are handling clients concurrently. This is already built into your CLI with the `--delay` flag.
@@ -108,3 +101,5 @@ To submit your code, upload it to Gradescope.
0 commit comments