Skip to content

Instantly share code, notes, and snippets.

@yuliji
yuliji / multi_thread_queue.py
Created August 28, 2020 01:28
[queue multithread] Multithread queue printing in one thread
import Queue # or queue in Python 3
import threading
class PrintThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def printfiles(self, p):
for path, dirs, files in os.walk(p):
@yuliji
yuliji / thread_lock.py
Created August 28, 2020 00:38
[multithread lock] Python multithread lock
import threading
list1Lock = threading.Lock()
with list1Lock:
# do sth
@yuliji
yuliji / promise.js
Created August 12, 2020 01:53
[promise]
const fs = require('fs')
const readFilePromise = (...args) => {
return new Promise((resolve, reject) => {
fs.readFile(...args, (err, data) => {
if (err) return reject(err)
resolve(data)
})
})
}
@yuliji
yuliji / countasync.py
Created July 29, 2020 06:31
[async counter] Python couter async
# countasync.py
import asyncio
async def count():
print("One")
await asyncio.sleep(1)
print("Two")
async def main():
@yuliji
yuliji / ticker.go
Created July 22, 2020 05:37
[golag ticker]
package main
import (
"fmt"
"time"
)
func main() {
c := time.Tick(5 * time.Second)
for t := range c {
@yuliji
yuliji / timeout.go
Created July 22, 2020 05:32
[golang timeout]
package main
import (
"fmt"
"time"
)
func main() {
for {
select {
@yuliji
yuliji / multi_thread.py
Created July 20, 2020 12:29
[multi thread python]
#!/usr/bin/env python
import threading
def resubmit_dlq():
do_somethings()
@yuliji
yuliji / boto3_iam_role.py
Created July 20, 2020 11:59
[boto3_iam_role.py] boto3 use ec2 iam profile role, boto3 sqs client
session = boto3.Session()
credentials = session.get_credentials()
# Credentials are refreshable, so accessing your access key / secret key
# separately can lead to a race condition. Use this to get an actual matched
# set.
#credentials = credentials.get_frozen_credentials()
#access_key = credentials.access_key
#secret_key = credentials.secret_key
@yuliji
yuliji / build_go.sh
Created July 17, 2020 01:31
[build golang] Build golang embed version and cross compile
main() {
if [[ "$*" == "-h" || "$*" == "--help" ]]; then
usage
exit
fi
local os
for os in darwin linux; do
log_info "Building roo for ${os}"
env GOOS="${os}" GOARCH=amd64 go build -o bin/roo-${os}-amd64 -ldflags="-X 'github.com/Canva/roo/pkg/cli.BuildCommit=lalalala'" github.com/Canva/roo/pkg/cmd/roo
done
@yuliji
yuliji / test_http.go
Last active July 15, 2020 05:36
[mock http unit test] Golang unit test with mock http #go
package manifest
import (
"net/http/httptest"
)
func makeTestServer(statusCode int, responseBody string) *httptest.Server {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
_, _ = w.Write([]byte(responseBody))