-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (48 loc) · 1.53 KB
/
main.py
File metadata and controls
59 lines (48 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# import requests
from kafka import KafkaProducer
from kafka_wrapper import Buffer
from constants import BOOTSTRAP_SERVER, TOPIC
# Set up a Kafka producer.
producer = KafkaProducer(bootstrap_servers=BOOTSTRAP_SERVER)
# Set up a queue.
buffer = Buffer(topic=TOPIC, producer=producer)
# push a function call.
def multiply(a, b):
return a * b
def sum(a, b):
return a + b
# import time
# start = time.time()
# async def sleep():
# print(f'Time: {time.time() - start:.2f}')
# await time.sleep(1)
# async def sum(name, numbers):
# total = 0
# for number in numbers:
# print(f'Task {name}: Computing {total}+{number}')
# await sleep()
# total += number
# print(f'Task {name}: Sum = {total}\n')
import asyncio
from time import sleep
def job1():
print("Starting job 1")
sleep(2)
print("Job 1 completed")
def job2():
print("Starting job 2")
sleep(1)
print("Job 2 completed")
def dummy_main():
print("Starting main")
[job1(), job2()]
print("Main completed")
# job = buffer.push(requests.get, "https://google.com")
job = buffer.push(sum, 2, 5) # 10 # 7
# job = buffer.using(partition=1).push(sum, 3, 5) # 15 # 8
# job = buffer.using(partition=2).push(sum, 4, 5) # 20 # 9
# job = buffer.using(partition=3).push(sum, 5, 5) # 25 # 10
# job = buffer.push(asyncio.run, dummy_main())
# job = buffer.push(dummy_main)
# You can also specify the job timeout, Kafka message key and partition.
# job = buffer.using(timeout=5, key=b"foo", partition=0).push(requests.get, "https://google.com")