为什么我不能在线程外调用函数?我怎么称呼它?

时间:2016-04-01 09:12:03

标签: python multithreading python-3.x python-multithreading

我正在尝试调用我在线程之外的函数。

我有一个线程函数从互联网上提取数据并将其放在数据库中。

我需要将这些数据保存到DB后发送给注册用户,我有一个发送它的功能,但是我无法调用它,我不知道为什么......我从未使用过以前的线程...有人可以帮助我......

这是发送给注册用户的功能:

def sM(chatId, message):
    yield from bot.sendMessage(chatId, '<code>{}</code>'.format(message), parse_mode='HTML');

我需要在getJSON函数中调用它,但我不知道如何...

以下是整个代码:

def sM(chatId, message):
    yield from bot.sendMessage(chatId, '<code>{}</code>'.format(message), parse_mode='HTML');

def getJSON():
    db = Connect('bot.db');
    ultimoCadastrado = '';
    while 1:
        try:
            getJson = requests.get('https://api.import.io/store/connector/0f73e47a-bc1d-490b-924b-8886ac703701/_query?input=webpage/url:https%3A%2F%2Fwww.gatry.com%2F&&_apikey=000c112af5ad4f33b2f40fdfe34ec36ba64eea2cfb0998639867a151bdaf65f498dc0376a779863af592d87e58c3291732681793b89008f48fbc545a9cb4c34eb6ebc3b5a5695508a964ed5cdda7eb9d');
            data = json.loads(getJson.text);

            if('results' in data):
                nomeP = data['results'][0]['nome'];
                valorP = data['results'][0]['valor'];
                linkP = data['results'][0]['link_gatry'];
                findData = re.search('title=\"([^\"]+)', str(data), re.I);
                dataP = findData.group(1);
                if(nomeP != ultimoCadastrado):
                    print('Promoção encontrada! -> ', nomeP);
                    print('Verificando em nossos bancos de dados...');
                    if not(db.checkPromoDuplicate(nomeP, valorP, dataP, linkP)):
                        db.addPromo(nomeP, valorP, dataP, linkP);
''' i need to call it here->            sM('111111', 'Hello i'm calling');'''
                    ultimoCadastrado = nomeP;

            else:
                print('KeyError: a chave não foi encontrada no JSON.');
        except:
            pass

        time.sleep(10);


TOKEN = sys.argv[1];  # get token from command-line

bot = telepot.async.Bot(TOKEN)
loop = asyncio.get_event_loop()
botDB = Connect('bot.db');

threadJson = Thread(target = getJSON);
threadJson.setDaemon(True);
threadJson.start();

loop.create_task(bot.messageLoop(on_chat_message))
print('Listening ...')

loop.run_forever()

提前致谢,对我的英语或代码感到抱歉......我正在学习......

0 个答案:

没有答案