N
N
Noob_master692020-05-10 11:13:38
Python
Noob_master69, 2020-05-10 11:13:38

How to host multiple worker programs in one Heroku app at once?

And so, there are several parsers that interact with the VK bot, that is, data is collected from different sites and transmitted to the bot. The bot will be hosted on Heroku, and I need 4 programs at once (3 parsers and the bot itself) to be running at the same time. The code of one of the parsers:

import requests as req
from bs4 import BeautifulSoup as BS
import json
import sched, time

s = sched.scheduler(time.time, time.sleep)

def get_ruble_course():
    s.enter(3600, 1, get_ruble_course)
    #для того, чтобы данные обновлялись
    r_usd = req.get('https://finance.rambler.ru/calculators/converter/1-USD-RUB/')
    html_usd = BS(r_usd.content, 'html.parser')

    value_usd = html_usd.select('.converter-display__value')
    dif_usd = html_usd.select('.converter-change-table__change')

    usd_course = value_usd[1].text
    usd_course_dif = dif_usd[0].text

   ruble_couse_data = {
       'usd_course' : usd_course,
       'usd_course_dif' : usd_course_dif,
   }

    with open('ruble_couse_data.json', 'w') as f:
       json.dump(ruble_couse_data, f)

    print('Ruble course data was successfully updated')

get_ruble_course()
s.run()

Bot code:
#import vars from Heroku
import os
token = os.environ['TOKEN']
group_id = os.environ['GROUP_ID']

#import lib for vk bot
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

#login to system as club bot
vk_session = vk_api.VkApi(token=token)
longpoll = VkBotLongPoll(vk_session, group_id)

#to get methods
vk = vk_session.get_api()

with open('ruble_course_data.json', 'r') as ruble_file:
    ruble_data = json.load(ruble_file)

#ruble course message send
rc_message = 'Узнать курс рубля: \n 1 доллар = {0} рублей\n\
   Изменение:{1}'.format(ruble_data['usd_course'], ruble_data['usd_course_dif'])

def main():

    for event in longpoll.listen():

        #if new message was got
        if event.type == VkBotEventType.MESSAGE_NEW:
            message = event.obj['message']
            user = message['from_id']
            text = message['text']

            if text == 'Узнать курс рубля':
                vk.messages.send(peer_id = user, message=rc_message,\
                    random_id = 0)

print("Bot is running...")

#run mainloop
if __name__ == '__main__':
    main()

What do you advise?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question