Skip to content

Instantly share code, notes, and snippets.

View pingrunhuang's full-sized avatar

Frank Huang pingrunhuang

View GitHub Profile
@pingrunhuang
pingrunhuang / README.md
Created November 2, 2024 14:54
2 ways of raising exception out from sub threads
  • ThreadPoolExecutor is good for when you don't want to re-create threads over and over again. Better if you have a looping task that needs to be ran in a sub threads
  • Threading & queue is good for simplicity that you have a one off task
# yield 和 join 使用
### yield方法
暂停当前正在执行的线程对象。
yield()方法是停止当前线程,让同等优先权的线程或更高优先级的线程有执行的机会。
如果没有的话,那么yield()方法将不会起作用,并且由可执行状态后马上又被执行。
### join是Thread方法
join方法适用于在某一个线程的执行过程中调用另一个线程执行,等到被调用的线程执行结束后,再继续执行当前线程。如:t.join();
主要用于等待t线程运行结束,若无此句,main则会执行完毕,导致结果不可预测。
abstract class Monoid[A] {
def add(x: A, y: A): A
def unit: A
}
object ImplicitTest {
implicit val stringMonoid: Monoid[String] = new Monoid[String] {
def add(x: String, y: String): String = x concat y
def unit: String = ""
}
@pingrunhuang
pingrunhuang / context_manager_1.py
Last active August 8, 2018 08:02
context manager collections
"""
Note from https://jeffknupp.com/blog/2016/03/07/python-with-context-managers/
"""
# first way of creating a context manager
class File():
def __init__(self, filename, mode):
self.filename=filename
self.mode=mode
@pingrunhuang
pingrunhuang / tree.md
Created July 30, 2018 16:13 — forked from hrldcpr/tree.md
one-line tree in python

One-line Tree in Python

Using Python's built-in defaultdict we can easily define a tree data structure:

def tree(): return defaultdict(tree)

That's it!

@pingrunhuang
pingrunhuang / Count.py
Last active July 28, 2018 16:58
Generator and Coroutine
class Count(object):
def __init__(self, start=0, step=1, stop=10):
self.n = start
self.step = step
self.stop = stop
def __next__(self):
n = self.n
if n>self.stop:
raise StopIteration()
SELECT "运单号", "sum"("开单金额"), "收货本部"
FROM express_2016_01
GROUP BY "运单号", "收货本部"
ORDER BY "sum"("开单金额") DESC
LIMIT 10
RUN curl -LO 'http://download.oracle.com/otn-pub/java/jdk/7u71-b14/jdk-7u71-linux-x64.rpm' -H 'Cookie: oraclelicense=accept-securebackup-cookie'
RUN rpm -i jdk-7u71-linux-x64.rpm
RUN rm jdk-7u71-linux-x64.rpm
ENV JAVA_HOME /usr/java/default
ENV PATH $PATH:$JAVA_HOME/bin
RUN rm /usr/bin/java && ln -s $JAVA_HOME/bin/java /usr/bin/java
@pingrunhuang
pingrunhuang / channel.go
Created April 24, 2018 12:24
Learning golang
package main
import "fmt"
// channel as argument
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
import logging
import threading
import time
logging.basicConfig(level=logging.DEBUG,
format='[%(levelname)s] (%(threadName)-10s) %(message)s',
)
def worker():
logging.debug('Starting')