为了账号安全,请及时绑定邮箱和手机立即绑定

如何在Python中的线程之间共享数组索引?

如何在Python中的线程之间共享数组索引?

阿晨1998 2023-08-08 15:09:19
我有以下代码:def task1():    for url in splitarr[0]:        print(url) #these are supposed to be scrape_induvidual_page() . print is just for debuggingdef task2():    for url in splitarr[1]:        print(url)def task3():    for url in splitarr[2]:        print(url)def task4():    for url in splitarr[3]:        print(url)def task5():    for url in splitarr[4]:        print(url)def task6():    for url in splitarr[5]:        print(url)def task7():    for url in splitarr[6]:        print(url)     def task8():    for url in splitarr[7]:        print(url)   splitarr=np.array_split(urllist, 8)t1 = threading.Thread(target=task1, name='t1') t2 = threading.Thread(target=task2, name='t2')   t3 = threading.Thread(target=task3, name='t3')t4 = threading.Thread(target=task4, name='t4') t5 = threading.Thread(target=task5, name='t5')t6 = threading.Thread(target=task6, name='t6')t7 = threading.Thread(target=task7, name='t7')t8 = threading.Thread(target=task8, name='t8')t1.start() t2.start()t3.start() t4.start() t5.start()t6.start() t7.start()t8.start() t1.join()t2.join()t3.join()t4.join()t5.join()t6.join()t7.join() t8.join() 它确实有所需的输出,没有重复或任何东西https://kickasstorrents.to/big-buck-bunny-1080p-h264-aac-5-1-tntvillage-t115783.htmlhttps://kickasstorrents.to/big-buck-bunny-4k-uhd-hfr-60fps-eng-flac-webdl-2160p-x264-zmachine-t1041079.htmlhttps://kickasstorrents.to/big-buck-bunny-4k-uhd-hfr-60-fps-flac-webrip-2160p-x265-zmachine-t1041689.htmlhttps://kickasstorrents.to/big-buck-bunny-2008-720p-bluray-x264-don-no-rars-t11623.htmlhttps://kickasstorrents.to/tkillaahh-big-buck-bunny-dvd-720p-2lions-team-t87503.htmlhttps://kickasstorrents.to/big-buck-bunny-2008-720p-bluray-nhd-x264-nhanc3-t127050.htmlhttps://kickasstorrents.to/big-buck-bunny-2008-brrip-720p-x264-mitzep-t172753.html但是,我觉得代码对于所有重复的def taskx() 来说有点多余: 所以我尝试使用单个任务来压缩代码:x=0def task1():    global x    for url in splitarr[x]:        print(url)        x=x+1如何在多线程程序中正确地使 x 递增?
查看完整描述

1 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

for url in splitarr[x]:为 中的序列创建一个迭代器splitarr[x]。稍后增加 x 并不重要 - 迭代器已经构建好了。由于其中有打印内容,因此所有线程很可能会x在其仍为零时抓取并迭代相同的序列。


args一种解决方案是通过中的参数将递增值传递给 task1 threading.Thread。但线程池更容易。


from multiprocessing.pool import ThreadPool


# generate test array

splitarr = []

for i in range(8):

    splitarr.append([f"url_{i}_{j}" for j in range(4)])


def task(splitarr_column):

    for url in splitarr_column:

        print(url)


with ThreadPool(len(splitarr)) as pool:

    result = pool.map(task, splitarr)

在此示例中,len(splitarr)用于为 中的每个序列创建一个线程splitarr。然后将每个序列映射到该task函数。由于我们创建了正确数量的线程来处理所有序列,因此它们都会同时运行。当映射完成时,该with子句退出并且池关闭,从而终止线程。


查看完整回答
反对 回复 2023-08-08
  • 1 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信