-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool_handler.py
44 lines (35 loc) · 1.03 KB
/
pool_handler.py
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
from multiprocessing import Pool
import os
import time
from main import StockManager
import asyncio
import logging
stocks = ["NVDA, MSFT"]
def worker(stock_name):
time.sleep(1)
async def main():
stock = StockManager(stock_name)
try:
# stock.login()
stock.get_current_price()
stock.get_stock_worth()
stock.analyze_stock()
except Exception as e:
print(stock.errors)
finally:
stock.close()
asyncio.run(main())
if __name__ == "__main__":
with Pool(processes=2) as pool:
results = []
for i in range(len(stocks)):
result = pool.apply_async(worker, args=(stocks[i],))
results.append(result)
pool.close()
pool.join()
print("All jobs started")
for result in results:
print("Job completed")
print("Finished job", os.getpid())
time.sleep(1) # Sleep for 1 second to avoid overloading the API
print("All jobs completed")