Reputation: 65
How do I pull data from a thread that's located within a process? I'm trying to start multiple processes and within each process, create threads to do work. My multithreading knowledge is very limited and this is as far as I can get.
import concurrent.futures as cf
import multiprocessing as mp
import subprocess
from dataclasses import dataclass
# Ping Output:
'''
Pinging google.com [142.250.113.100] with 32 bytes of data:
Reply from 142.250.113.100: bytes=32 time=26ms TTL=104
Reply from 142.250.113.100: bytes=32 time=26ms TTL=104
Reply from 142.250.113.100: bytes=32 time=27ms TTL=104
Reply from 142.250.113.100: bytes=32 time=26ms TTL=104
Ping statistics for 142.250.113.100:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 26ms, Maximum = 27ms, Average = 26ms
'''
@dataclass(kw_only=True)
class Ping():
name: str
ip: str
response: bool = False
def ping(target):
cmd = subprocess.run(f"ping -n 2 {target}", shell=True, capture_output=True)
out = cmd.stdout.decode()
for line in out.splitlines():
if "Reply from " in line:
line = line.split(" ")
input = Ping(name=f'{target}',ip=line[2][:-1], response=True)
output.append(input) # Trying to append data to the output list.
break
def thread(targets):
with cf.ThreadPoolExecutor(max_workers=32) as execute:
results = [execute.submit(ping, target) for target in targets]
targets = ["google.com", "amazon.com"]
output = []
if __name__ == "__main__":
p = mp.Process(target=thread, args=(targets,))
p.start()
p.join()
print(output) # This will not output any data from the ping function.
From this code, I want to append formatted data from the ping function to a list. If I do the same thing without using mp.Process(), it works. Any help is greatly appreciated!
EDIT:
import concurrent.futures as cf
import multiprocessing as mp
import queue
def count(num, qq):
qq.put(num)
def thread(numbers, q):
qq = queue.Queue()
with cf.ThreadPoolExecutor(max_workers=32) as execute:
results = [execute.submit(count, num, qq) for num in numbers]
q.put(qq.get())
qq.task_done()
numbers = ['one', 'two', 'three']
if __name__ == "__main__":
q = mp.Queue()
p = mp.Process(target=thread, args=(numbers,q))
p.start()
p.join()
print(q.get()) # This should contain 'one', 'two', 'three'.
Upvotes: 1
Views: 107