I choose Golang to implement the API for two reasons. First, this is a challenge, it will be boring if I keep using my old stack. Second, Golang makes writing a concurrent program easily which is what the requirements ask for.
1. The API must be able to handle high throughput (~1k requests per second).
2. The API should also be able to recover and restart if it unexpectedly crashes.
3. Assume that the API will be running on a small machine with 1 CPU and 512MB of RAM.
For No.1 and No.3, we need to use 1 CPU and 512MB of RAM to handle ~1k requests per second. Therefore, asynchronous I/O would be the best choice. Golang has goroutine which is a perfect fit for this situation. To support restart, we can use Supervisord to monitor the current Go web server, I didn't implement this part because it's more related to configuration than system design (I used it before, feel free to ask me questions for it). To support recovery, for instance,
-
function call stack
When I'm new to a project or repo, lots of time I get lost How A function call D function (e.g. foo function calls exa function and exa function calls bar function). After I understand How D function works by code search. I forgot about what A function does already. It will to great if we can add the call stack from A function to D function so developers would not get lost. For example:
- Understand the projects
When I use code search
, what I want is have a better understanding of the project I'm working on. If this is one of the missions from Sourcegraph, we can create some handly tools for developers. For example,
建议先完成第一题 Permutations:
解法一:使用一个 hashtable 对象或者 set 对象,重复的元素不要放到结果中。只需要在第一题的基础加上一个判断即可。
解法二:我们可以观察到,解法一在数组中包含多个重复元素的时候会有很多重复遍历,所以在最开始的遍历时,跳过重复元素,举例:
Input: [1,1,2] Output: [ [1,1,2],
建议先完成 Subarray Sum Equals K
-
我们可以观察到对于任意数组 sum(array[i, j]) = sum(array[0, j] - sum(array[0, i-1])
-
所以我们可以先创建一个 presum 数组,其中 presum[i] = sum(A[0,i]),例如 array = [1, 2, 3], 则 presum = [1, 3, 6]
-
根据 0,我们可以得到 sum(array[i, j]) = presum[j] - presum[i-1]
array[1:2] = 5 = presum[2] - presum[0] array[1:1] = 3 = presum[1] - presum[0]
gcc -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fmax-type-align=8 -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -DPy_BUILD_CORE \ | |
-DABIFLAGS='""' \ | |
-DMULTIARCH=\"darwin\" \ | |
-o Python/sysmodule.o ./Python/sysmodule.c | |
gcc -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fmax-type-align=8 -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -DPy_BUILD_CORE \ | |
-DSOABI='"cpython-38-darwin"' \ | |
-o Python/dynload_shlib.o ./Python/dynload_shlib.c | |
gcc -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fmax-type-align=8 -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstri |
gcc -c -Wno-unused-result -Wsign-compare -g -O0 -Wall -fmax-type-align=8 -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Programs/python.o ./Programs/python.c | |
gcc -c -Wno-unused-result -Wsign-compare -g -O0 -Wall -fmax-type-align=8 -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Parser/acceler.o Parser/acceler.c | |
gcc -c -Wno-unused-result -Wsign-compare -g -O0 -Wall -fmax-type-align=8 -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -I./Include/internal -I. -I./Include -DPy_BUILD_CORE -o Parser/grammar1.o Parser/grammar1.c | |
gcc -c -Wno-unused-result -Wsign-compare -g -O0 -Wall -fmax-type |
class Pupil_Recording(System_Plugin_Base): | |
""" | |
Collect log in recording | |
""" | |
def __init__(self, g_pool): | |
super().__init__(g_pool) | |
self.order = 0.08 | |
self.notify_sub = zmq_tools.Msg_Receiver( | |
zmq_ctx, ipc_sub_url, topics=("notify",)) |
import os | |
import subprocess | |
import logging | |
from signal import SIGTERM | |
# Setup logging | |
logger = logging.getLogger('__name__') | |
logger.setLevel(logging.INFO) | |
# Handler | |
stream = logging.StreamHandler() |
** A. CalledProcessError (only the first time)** | |
Reproduce | |
1. run `python main.py player` | |
2. use 001 as recording files | |
3. run `Redetect` in `Offline Pupil Detection` | |
(pupil) windson@WindsondeAir ~/learn/pupil/pupil_src (v1.11-preview) $ python main.py player | |
MainProcess - [INFO] os_utils: Disabled idle sleep. |