Aspect | Cooperative Multitasking (General) | Go's Concurrency Model (Goroutines and Channels) |
---|---|---|
Control Transfer | Tasks yield control voluntarily. | Goroutines yield control voluntarily or at specific points. They can also explicitly yield using runtime.Gosched() . |
Scheduling Responsibility | Tasks are responsible for yielding control. | Go's runtime scheduler is responsible for managing goroutine execution. |
Task Cooperation | Tasks must cooperate to ensure fairness. | Goroutines cooperate by yielding control, but the scheduler can preemptively switch them as well. |
Deadlock Risk | Risk of deadlock if tasks don't yield. | Similar risk of deadlock if goroutines don't yield or release resources. |
Resource Sharing | Resource sharing |
Property | Group | Monoid | Semigroup |
---|---|---|---|
Closure | Closed under binary operation | Closed under binary operation | Closed under binary operation |
Associativity | Operation is associative | Operation is associative | Operation is associative |
Identity | Exists an identity element | Exists an identity element | Identity might not be present |
Inverse | Each element has an inverse | Inverses might not be present | Inverses might not be present |
Examples:
- Group:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Acquire::http::Proxy { | |
download.docker.com "http://localhost:8118"; | |
}; | |
Acquire::https::Proxy { | |
download.docker.com "http://localhost:8118"; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
"start", | |
"end", | |
version, | |
COUNT(*) AS num_segments, | |
AVG("num_rows") AS avg_num_rows, | |
MIN("num_rows") AS min_num_rows, | |
max("num_rows") AS max_num_rows, | |
SUM("num_rows") AS total_num_rows, | |
MIN("size") / 1024 / 1024 AS min_size, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageDraw, ImageFont | |
def get_text_dimensions(text_string, font): | |
# https://stackoverflow.com/a/46220683/9263761 | |
ascent, descent = font.getmetrics() | |
text_width = font.getmask(text_string).getbbox()[2] | |
text_height = font.getmask(text_string).getbbox()[3] + descent | |
return (text_width, text_height) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# See: https://github.com/provectus/kafka-ui | |
version: '2' | |
services: | |
kafka-ui: | |
image: provectuslabs/kafka-ui | |
container_name: kafka-ui | |
restart: always | |
network_mode: host | |
environment: | |
- SERVER_PORT=6969 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Check system | |
if [ ! -f /etc/lsb-release ];then | |
if ! grep -Eqi "ubuntu|debian" /etc/issue;then | |
echo "\033[1;31mOnly Ubuntu or Debian can run this shell.\033[0m" | |
exit 1 | |
fi | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.language.postfixOps | |
class VectorClock(private var name: String, private var countMap: Map[String, Int] = Map()) { | |
if (!countMap.isDefinedAt(name)) countMap = countMap ++ Map(name -> 1) | |
// increment | |
def ++ = VectorClock( | |
name, | |
countMap ++ Map(name -> (countMap.getOrElse(name, 0) + 1)) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.language.postfixOps | |
class LamportClock(private var count: Int = 0) { | |
// increment | |
def ++ = LamportClock(count + 1) | |
// merge | |
def +(other: LamportClock) = new LamportClock(math.max(count, other.count) + 1) | |
override def toString: String = s"LamportClock($count)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import datetime | |
from kafka import KafkaProducer | |
from json import dumps | |
from kafka.errors import KafkaError | |
def produce(): | |
producer = KafkaProducer(bootstrap_servers=['localhost:9092'], value_serializer=lambda x: dumps(x).encode('utf-8')) | |
response = requests.get('https://api.coingecko.com/api/v3/exchange_rates').json()['rates'] |