Created
November 12, 2017 21:46
-
-
Save jarhill0/811b297770513b8684cc18d2c4d38321 to your computer and use it in GitHub Desktop.
Telepot not playing nice with multiprocessing
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 multiprocessing | |
import random | |
import time | |
import telepot | |
from telepot.loop import MessageLoop | |
import config | |
bot = telepot.Bot(config.token) | |
def long_process(u_id, msg): | |
"""A process that takes between 3 and 10 seconds.""" | |
slptime = random.randrange(3, 10) | |
print('long_process({}, {}) sleeping for {} at {}'.format(u_id, msg, slptime, int(time.time() % 1000))) | |
time.sleep(slptime) | |
print('long_process({}, {}) sending message at {}'.format(u_id, msg, int(time.time() % 1000))) | |
bot.sendMessage(u_id, 'This is a response to {}.'.format(msg)) | |
print('long_process({}, {}) sent message at {}'.format(u_id, msg, int(time.time() % 1000))) | |
def handle(msg): | |
user_id = msg['chat']['id'] | |
command = msg['text'] | |
multiprocessing.Process(target=long_process, args=[user_id, command]).start() | |
MessageLoop(bot, handle).run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment