使用带有锁定列表的Python 3.6线程

时间:2019-03-19 19:19:35

标签: python python-3.x multithreading locks

我有一个在各种启动条件下一直使用的可执行文件。根据我提供的输入范围,它可能运行数百次。加快此速度是当务之急,因此,我尝试使用线程和锁列表。

锁定列表的原因是为了限制6核CPU的总负载。因此,我为该帖子选择了3个锁,但我计划使用5个。

我正在显示三种不同的尝试,每种尝试都以不同的方式失败。

  1. 输入范围永远不会扩大,即在程序退出之前仅使用第一个组合。 (循环遍历lock_list,如果lock.locked():中断)
  2. 每个输入运行n_locks次。 (遍历lock_list)
  3. 实际上并没有运行。 (在lock_list中的每次锁定之后中断)

我没有显示实际的程序调用只是我尝试实现Thread的方式。

import threading
import time
import random

# Random seed for reproducibility
random.seed(123)

# Limit the number of active threads using locks
n_locks = 3
lock_list = list()

# Create a list that holds the desired number of locks
for i in range(n_locks):
    lock_list.append(threading.Lock())

# Some dummy data for use in the function to be threaded
aa = [1, 2, 3, 4, 5, 6, 7, 8]
bb = [3, 4, 3, 4, 3, 4, 3, 4]

# The function to be used, where execution time may vary
def multiply(a, b):
    print(a, b, a * b)
    time.sleep(random.random() * 0.5)


# Show the inputs and outputs without threading
print('Unthreaded output...')

for d1, d2 in zip(aa, bb):
    multiply(d1, d2)

# The same function, but using 'with lock:'
def multiply_threaded(l, a, b):
    with l:
        print(a, b, a * b)
        time.sleep(random.random() * 1.0)


# Here's where I'm having a problem...
print('\nThreaded Ouptut, 1st Try')
for d1, d2 in zip(aa, bb):
    for lk in lock_list:
        if lk.locked():
            break
        else:
            threading.Thread(target=multiply_threaded, args=(lk, d1, d2)).start()

time.sleep(1.5)
print('The data set is never advanced...')
# Sleep to wait for the 2nd attempt...

# 2nd Try
print('\nThreaded Ouptut, 2nd Try')
for d1, d2 in zip(aa, bb):
    for lk in lock_list:
        threading.Thread(target=multiply_threaded, args=(lk, d1, d2)).start()

time.sleep(5.5)
print('The data set is used 3 times...')


# 3rd Try
print('\nThreaded Ouptut, 3rd Try')
for d1, d2 in zip(aa, bb):
    for lk in lock_list:
        threading.Thread(target=multiply_threaded, args=(lk, d1, d2)).start()
        break

time.sleep(5.5)
print("Doesn't appear to be executed concurrently...")

下面是输出:

Unthreaded output...
1 3 3
2 4 8
3 3 9
4 4 16
5 3 15
6 4 24
7 3 21
8 4 32

Threaded Ouptut, 1st Try
1 3 3
1 3 3
1 3 3
The data set is never advanced...

Threaded Ouptut, 2nd Try
1 3 3
1 3 3
1 3 3
2 4 8
2 4 8
2 4 8
3 3 9
4 4 16
3 3 9
5 3 15
4 4 16
3 3 9
5 3 15
4 4 16
5 3 15
6 4 24
6 4 24
6 4 24
7 3 21
7 3 21
8 4 32
8 4 32
7 3 21
8 4 32
The data set is used 3 times...

Threaded Ouptut, 3rd Try
1 3 3
2 4 8
3 3 9
4 4 16
5 3 15
6 4 24
7 3 21
8 4 32
Doesn't appear to be executed concurrently...

Process finished with exit code 0

我希望看到的内容与非线程输出相似,并尝试#3,不同之处在于该顺序不必一定是输入的顺序,因为sleep函数用于模拟可执行文件的加载。

感谢您抽出宝贵的时间阅读!

编辑:我在实际线程中使用os.system(“ C:\ my_exe.exe> C:\ my_output.txt)。当它们使用时,我无法访问exe或支持文件,所以我已经制作副本,以便每个线程可以使用一个。d

0 个答案:

没有答案