Python Parallel Programming Video 12. Python process communication by using Queue.

preview_player
Показать описание
Python multiprocessing communication
Exchanging object through the communication Channel
Process with Queue
Producer and consumer problem with queue
Рекомендации по теме
Комментарии
Автор

Hi Mohit sir, which one of your book should I buy for python to learn more.

muneshj
Автор

I'm trying to use multiprocessing's queue to send data, but it never seems to get the data. Any ideas?


File #1 (main.py):

from matplotlib_graph as mplive
from time import time, sleep
from multiprocessing import Queue as mpQueue

if __name__=='__main__':
try:
# Shared Memory
q = mpQueue()

# Start Plotter
mpInst = mplive.PlotClass( q )
mpInst.run()

# Push Data to Plotter
while True:
q.put(1)
sleep(1)

except KeyboardInterrupt:
print ("Keyboard Interrupt!")




File #2 (matplotlib_graph.py):

import matplotlib.pyplot as plt
import matplotlib.animation as animation
global ani # **Must be assigned to a global variable****

class PlotClass:
# Create *STATIC LOCAL* instances accessible across class functions
fig = None
ax = None
line1 = None
q = None
x_list_vec = []
y1_list_vec = []

def __init__(self, qdata ): __#Class__ Object __init__
# Save handle to Queue
self.q = qdata

# this is the call to matplotlib that allows dynamic plotting
plt.ion()

# Setup Figure with 'size' number of elements
self.fig = plt.figure(figsize=(13, 6))
self.ax = self.fig.add_subplot(111)

# Create a list variable to the animation
self.line1, = self.ax.plot(self.x_list_vec, self.y1_list_vec, marker = 'o', label='y1', color='r')

#Show a starting plot
plt.ion()
plt.xticks(rotation=45, ha='right')
plt.title('Title: Hello World')

plt.show()

def update(self, frame_num, user_arg1, user_arg2):
while self.q.empty() is False:
print ("Got Data!") #<<<< Never Executes!!!
print ( self.q.get() )

#Dummy Draw
self.line1.set_data(0, 6)

#Must return list when blit=True
return self.line1,

# Override the run function of Thread class
def run(self):
# **Must be assigned to a global variable!****
global ani
ani = animation.FuncAnimation(fig=self.fig, func=self.update, fargs=["Hi", 2], interval=0.1, blit=True, repeat=False)

bennguyen
welcome to shbcf.ru