在多线程中使用队列 returns 函数地址
Using Queue in Multi-Threading returns address of function
我正在尝试实现一个多线程程序,该程序在多个线程上找出素数并汇总它们的结果。
我正在使用队列实际存储返回值。但是当我打印队列的值时,只打印函数的地址
n = int(input("Enter the value:"))
def task1():
global n
print("Task 1 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 1: {}".format(os.getpid()))
a=[]
for i in range(2,n//2):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
return a
def task2():
global n
print("Task 2 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 2: {}".format(os.getpid()))
a=[]
for i in range(n//2+1,n):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
return a
import queue
que=queue.Queue()
t1 = threading.Thread(target=lambda q: q.put(task1), args=(que,), name='t1')
t2 = threading.Thread(target=lambda q: q.put(task2), args=(que,), name='t2')
# starting threads Spawn
t1.start()
t2.start()
# wait until all threads finish Sync
t1.join()
t2.join()
while not que.empty():
result=que.get()
print(result)
在 运行 代码之后,这是我得到的值
<function task1 at 0x00000204A9A11438>
<function task2 at 0x00000204A9A11288>
我需要打印质数数组而不是地址。
您正在将 task1
和 taks2
(它们都是函数)放入队列中:
.put(task1)
t1 = threading.Thread(target=lambda q: q.put(task1), args=(que,), name='t1')
所以当你读回它的内容时 - 你也会得到函数。
此外,即使您似乎打算将队列 que
作为参数传递,但函数 task1
和 taks2
是无参数的。我也不明白 lambda 在线程构造函数中的用法,但那是另一回事。
因为这看起来像是一项作业,所以我只是狭隘地回答你的问题,并没有提供完整的工作示例。
target
是线程中运行的函数。您让线程所做的(并且 仅 所做的)是向队列添加一个函数引用。你的任务不是运行.
修复:
- 为每个目标设置任务功能。
- 由于线程运行在同一个进程中,无需将其作为参数传递即可访问队列。与下面的锁相同。
- 将结果写入队列。
global n
不需要。仅当您想重新分配 n
. 时才需要
- 如果要在线程中打印,请创建一个锁来管理打印。否则可能会混淆。
- 添加适当的导入(为什么提问者总是不导入???)
您还会发现这可能 运行 比在一个非并行任务中简单地计算素数慢。 Python 进程被全局解释器锁 (GIL) 限制为 运行 Python 代码一次只能在一个线程中,因此如果线程 运行 CPU-绑定作品,这是。
线程是并行化 I/O 绑定工作的理想选择。通过 multiprocessing
模块使用进程到 运行 CPU 绑定任务,但请注意创建进程和进程间通信以将参数和结果发送回主进程的开销可以压倒像这样简单的 CPU 绑定操作,除非它们 运行 持续很长一段时间。
import threading
import os
import queue
n = int(input("Enter the value:"))
lock = threading.Lock()
que = queue.Queue()
def task1():
with lock:
print("Task 1 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 1: {}".format(os.getpid()))
a=[]
for i in range(2,n//2):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
que.put(a)
def task2():
with lock:
print("Task 2 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 2: {}".format(os.getpid()))
a=[]
for i in range(n//2+1,n):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
que.put(a)
t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')
# starting threads Spawn
t1.start()
t2.start()
# wait until all threads finish Sync
t1.join()
t2.join()
while not que.empty():
result=que.get()
print(result)
输出:
Enter the value: 100
Task 1 assigned to thread: t1
ID of process running task 1: 3864
Task 2 assigned to thread: t2
ID of process running task 2: 3864
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
[53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
我正在尝试实现一个多线程程序,该程序在多个线程上找出素数并汇总它们的结果。
我正在使用队列实际存储返回值。但是当我打印队列的值时,只打印函数的地址
n = int(input("Enter the value:"))
def task1():
global n
print("Task 1 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 1: {}".format(os.getpid()))
a=[]
for i in range(2,n//2):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
return a
def task2():
global n
print("Task 2 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 2: {}".format(os.getpid()))
a=[]
for i in range(n//2+1,n):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
return a
import queue
que=queue.Queue()
t1 = threading.Thread(target=lambda q: q.put(task1), args=(que,), name='t1')
t2 = threading.Thread(target=lambda q: q.put(task2), args=(que,), name='t2')
# starting threads Spawn
t1.start()
t2.start()
# wait until all threads finish Sync
t1.join()
t2.join()
while not que.empty():
result=que.get()
print(result)
在 运行 代码之后,这是我得到的值
<function task1 at 0x00000204A9A11438>
<function task2 at 0x00000204A9A11288>
我需要打印质数数组而不是地址。
您正在将 task1
和 taks2
(它们都是函数)放入队列中:
.put(task1)
t1 = threading.Thread(target=lambda q: q.put(task1), args=(que,), name='t1')
所以当你读回它的内容时 - 你也会得到函数。
此外,即使您似乎打算将队列 que
作为参数传递,但函数 task1
和 taks2
是无参数的。我也不明白 lambda 在线程构造函数中的用法,但那是另一回事。
因为这看起来像是一项作业,所以我只是狭隘地回答你的问题,并没有提供完整的工作示例。
target
是线程中运行的函数。您让线程所做的(并且 仅 所做的)是向队列添加一个函数引用。你的任务不是运行.
修复:
- 为每个目标设置任务功能。
- 由于线程运行在同一个进程中,无需将其作为参数传递即可访问队列。与下面的锁相同。
- 将结果写入队列。
global n
不需要。仅当您想重新分配n
. 时才需要
- 如果要在线程中打印,请创建一个锁来管理打印。否则可能会混淆。
- 添加适当的导入(为什么提问者总是不导入???)
您还会发现这可能 运行 比在一个非并行任务中简单地计算素数慢。 Python 进程被全局解释器锁 (GIL) 限制为 运行 Python 代码一次只能在一个线程中,因此如果线程 运行 CPU-绑定作品,这是。
线程是并行化 I/O 绑定工作的理想选择。通过 multiprocessing
模块使用进程到 运行 CPU 绑定任务,但请注意创建进程和进程间通信以将参数和结果发送回主进程的开销可以压倒像这样简单的 CPU 绑定操作,除非它们 运行 持续很长一段时间。
import threading
import os
import queue
n = int(input("Enter the value:"))
lock = threading.Lock()
que = queue.Queue()
def task1():
with lock:
print("Task 1 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 1: {}".format(os.getpid()))
a=[]
for i in range(2,n//2):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
que.put(a)
def task2():
with lock:
print("Task 2 assigned to thread: {}".format(threading.current_thread().name))
print("ID of process running task 2: {}".format(os.getpid()))
a=[]
for i in range(n//2+1,n):
c=0
for j in range(2,i+1):
if(i%j==0):
c+=1
if(c<=1):
a.append(i)
que.put(a)
t1 = threading.Thread(target=task1, name='t1')
t2 = threading.Thread(target=task2, name='t2')
# starting threads Spawn
t1.start()
t2.start()
# wait until all threads finish Sync
t1.join()
t2.join()
while not que.empty():
result=que.get()
print(result)
输出:
Enter the value: 100
Task 1 assigned to thread: t1
ID of process running task 1: 3864
Task 2 assigned to thread: t2
ID of process running task 2: 3864
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
[53, 59, 61, 67, 71, 73, 79, 83, 89, 97]