带嵌套列表循环的Python多线程

r1zhe5dt  于 2023-01-27  发布在  Python
关注(0)|答案(1)|浏览(117)

我想在python 3中使用多线程使我的程序更快。在这种情况下,我想在一秒钟内创建5个线程。所以这里的细节,
我有2个列表,其中包含一些唯一值

# list 1
countries = ['argentina', 'brazil', 'czech', 'denmark']

# list 2
# each country should have his own code below, so i make it into a dict
countries_seq = dict()
for country in countries:
   countries_seq[country] = [f'{country}{n}' for n in range(1000)]

# so we will get the result of for loop above,
# countries_seq = {'argentina': [....], 'brazil': [...], 'czech': [...], 'denmark': [...]}

我的目标是创建5个线程,将这2个列表传递给我的arguments函数,这里是我的函数代码

def submit_country(country_name: str, country_seq: str):
   # submit country code, here i request to an API
   print(f'{country_name} - {country_seq}')

这里是完整的代码,

def submit_country(country_name: str, country_seq: str):
   print(f'{country_name} - {country_seq}')

countries = ['argentina', 'brazil', 'czech', 'denmark']

countries_seq = dict()

for country in countries:
   countries_seq[country] = [f'{country}{n}' for n in range(1000)]

# multithreading will held here

问题是我如何创建具有5个线程的多线程,并且规则每个进程将向submit_country函数中的第一个参数传递一个国家名称,并向第二个参数传递5个country_seq代码,示例结果:

# First process
argentina - argentina0
argentina - argentina1
argentina - argentina2
argentina - argentina3
argentina - argentina4

# Second process
brazil - brazil0
brazil - brazil1
brazil - brazil2
brazil - brazil3
brazil - brazil4

# Third process
czech - czech0
czech - czech1
czech - czech2
czech - czech3
czech - czech4

# Fourth process
denmark - denmark0
denmark - denmark1
denmark - denmark2
denmark - denmark3
denmark - denmark4

# Fifth process - it will continue `argentina` process
argentina - argentina5
argentina - argentina6
argentina - argentina7
argentina - argentina8
argentina - argentina9

...
# until end

5.我一直在努力思考,但还是解不出来。5.这是我做这道题之前做的最后一个实验

from threading import Thread

def submit_country(country_name: str, country_seq: str):
   print(f'{country_name} - {country_seq}')

countries = ['argentina', 'brazil', 'czech', 'denmark']

countries_seq = dict()
for country in countries:
   countries_seq[country] = [f'{country}{n}' for n in range(1000)]

# multithreading will held here
for country in countries:
  for country_seq in countries_seq[country]:
    threads = []
    for tr in range(t):
      thread = Thread(target=submit_country, args=(country, country_seq))
      threads.append(thread)
      thread.start()
    
    for tr in threads:
      tr.join()

上面的代码将打印argentina及其country_seq,直到结束,然后继续打印brazilczechdenmark,直到完成。

0md85ypi

0md85ypi1#

用这个流程来求解,

from threading import Thread

def submit_country(country_name: str, country_seq: str):
   # request post API
   print(f'{country_name} - {country_seq}')

countries = ['argentina', 'brazil', 'czech', 'denmark']

countries_seq = dict()
for country in countries:
   countries_seq[country] = [f'{country}{n}' for n in range(1000)]

# multithreading will held here

max_thread = 5 # maximum thread
flag = True # flag in the future to stop while loop
start = 0 # initial slicing start
end = max_thread # initial slicing end

while flag:
   threads = []
   for country in countries:
      for country_seq in countries_seq[country][start:end]:
         thread = Thread(target=submit_country, args=(country, country_seq))
         threads.append(thread)
         thread.start()
     
      for thread in threads:
         thread.join()

      start = end
      end = end + max_thread
   
      if len(threads) < 1:
         flag = False

事实上,答案和提供的问题不一样。但是有了这个答案,我仍然得到了我想做的事情。

相关问题