在for循环中启动线程会产生多个结果

时间:2019-09-01 09:04:48

标签: python python-2.7

-我发现了问题!-在函数SendMessage中,我使用的是UserID(带有大写字母),而不是userid(这是传递给每个线程的实际参数)。因此,Python打印了for循环的UserID,而不是传递给不同函数的“单独”用户ID。只是一个日志问题,程序正确发送了消息。

我有一个 ,可以遍历用户列表的元素。每次迭代,我都想启动一个单独的后台线程来向该用户发送消息。说“发送消息”是指使用 requests Python库发出的简单POST请求。在线程末尾,将在控制台上写入输出。应用程序每24个请求(因此每24个线程)需要停止大约一秒钟。

select 
  t.c_id, companyname, t.c_firstname, t.c_lastname, t.empfirstname, t.emplastname
from (
  select 
    l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname, 
    emp.firstname as empfirstname, emp.lastname as emplastname 
  from tbl_lead as l inner join tbl_employee as emp 
  on l.createby=emp.id 
  WHERE l.leadstatus IN (2,3) 
  UNION ALL 
  select 
    l.date_of_created, l.c_id, l.companyname, l.c_firstname, l.c_lastname, 
    emp.firstname as empfirstname, emp.lastname as emplastname 
  from tbl_leadUpload as l inner join tbl_employee as emp 
  on l.createby=emp.id 
  where l.leadstatus IN (2,3) 
) as t
ORDER BY t.date_of_created DESC

比方说我的用户列表是: {1,2,3,4,...,24,25,...}。我希望我的应用程序输出:
1.消息 1 已成功发送...
2.消息 2 已成功发送...
...
24.消息 24 已成功发送。
相反,我得到以下输出:
1.消息 1 已成功发送。
2.消息 1 已成功发送。
...
24.消息 1 已成功发送。
因此,所有24个输出都与24个ID中的第一个相关。似乎for循环无法继续进行...

1 个答案:

答案 0 :(得分:0)

这将打印出递增的计数器而没有任何麻烦,因此我认为您可能需要提供所有代码和一些示例输入。

lexicons = {
    'direction': ['north', 'south', 'east', 'west'],
    'noun': ['car', 'bike'],
}

def scan(sentence):

    result = []
    words = sentence.split()

    for word in words:
        found = False
        for key, value in lexicons.items():
            if word in value:
                found = True
                break

        if found:
            result.append((key, word))
        else:
            result.append(('error', word))

    return result

scan('car bike QWER')

Run it on repl.it