I am trying to read 2 different files at the same time using pyqt and threads but only one thread gets run out of the two. My code has 2 thread classes and each are responsible for reading their assigned files. How can I achieve this?
Here is what I've tried:
import sys
from PyQt4 import QtCore, QtGui
import subprocess
from time import sleep
class Thread1(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def file_len(self):
p = subprocess.Popen(['wc', '-l', 'file1.txt'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result, err = p.communicate()
if p.returncode != 0:
raise IOError(err)
return int(result.strip().split()[0]) #returns 600 lines
def run(self):
self.emit(QtCore.SIGNAL('updateProgressBar(int)'), 0) ## Reset progressbar value
file_in = "file1.txt"
loading = 0
x = float(100) / self.file_len()
with open(file_in) as f:
for line in f:
loading += x
print line
self.emit(QtCore.SIGNAL('updateProgressBar(int)'), loading)
sleep(0.15)
class Thread2(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def file_len(self):
p = subprocess.Popen(['wc', '-l', 'file2.txt'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result, err = p.communicate()
if p.returncode != 0:
raise IOError(err)
return int(result.strip().split()[0]) #returns 2500 lines
def run(self):
self.emit(QtCore.SIGNAL('updateProgressBar(int)'), 0) ## Reset progressbar value
file_in = "file2.txt"
loading = 0
x = float(100) / self.file_len()
with open(file_in) as f:
for line in f:
loading += x
print line
self.emit(QtCore.SIGNAL('updateProgressBar(int)'), loading)
sleep(0.001)
class AppView(QtGui.QDialog):
def __init__(self, parent=None):
super(AppView, self).__init__(parent)
self.resize(400, 400)
self.buttonStart = QtGui.QPushButton(self)
self.buttonStart.setText("Start")
self.buttonStart.clicked.connect(self.start)
self.progress = QtGui.QProgressBar(self)
self.progress2 = QtGui.QProgressBar(self)
verticalLayout = QtGui.QVBoxLayout(self)
verticalLayout.addWidget(self.buttonStart)
verticalLayout.addWidget(self.progress)
verticalLayout.addWidget(self.progress2)
self.progressView = Thread1()
self.progressView2 = Thread2()
self.connect(self.progressView, QtCore.SIGNAL("updateProgressBar(int)"), self.updateProgressBar)
self.connect(self.progressView2, QtCore.SIGNAL("updateProgressBar2(int)"), self.updateProgressBar2)
self.start()
def updateProgressBar(self, percent):
self.progress.setValue(percent)
def updateProgressBar2(self, percent):
self.progress2.setValue(percent)
def start(self):
self.progressView.start()
self.progressView2.start()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
appview = AppView()
appview.show()
sys.exit(app.exec_())
Perhaps the method run of thread2 must call updateProgressBar2, not updateProgressBar?
Related
I have made a Desktop Application using Python and used PyQt5 and Pytube which could download video from youtube. When download is in Progress, I want to show user an animation. In Fact I did it, but when the file is getting downloaded the PyQt window seems like freezing and everything just gets paused until the download is complete. So, Does anyone know why is this happening? How do I fix it?
Here's the code snippet:
def download_created(self, qual): # Used in 'selection' method
selected_stream = yt.streams.get_by_resolution(qual)
self.progress_func()
try:
self.download_btn.setCurrentIndex(-1)
selected_stream.download(self.askLocation() + "/")
except:
pass
# This gets the quality that the user chooses
def selection(self):
global quality
quality = self.download_btn.currentText()
try:
self.download_created(quality) # Calls a method called 'download'
except:
self.start_anime()
# Fetching the details about the Link from Youtube
def download_youtube(self):
global check
if check != self.get_input():
check = self.get_input()
self.download_btn.clear()
enter_url = self.get_input()
try:
global yt
yt = pytube.YouTube(
enter_url,
on_progress_callback = on_progress,
on_complete_callback = self.complete_func)
self.start_anime()
except:
self.input_error()
VIDEO_TITLE = (yt.title)
global VIDEO_ID
VIDEO_ID = (yt.video_id)
videos = yt.streams.filter(mime_type="video/mp4", progressive="True")
# Display all the available qualities
for i in videos:
self.download_btn.addItem(i.resolution)
self.download_btn.currentIndexChanged.connect(self.selection)
You have to execute the time consuming tasks in another thread, for example in your case the task of getting the streams and downloading.
import sys
import threading
from functools import cached_property
from PyQt5 import QtCore, QtWidgets
import pytube
class QPyTube(QtCore.QObject):
initialized = QtCore.pyqtSignal(bool, str)
download_started = QtCore.pyqtSignal()
download_progress_changed = QtCore.pyqtSignal(int)
download_finished = QtCore.pyqtSignal()
def __init__(self, url):
super().__init__()
self._url = url
self._yt = None
self._mutex = threading.Lock()
threading.Thread(target=self._init, daemon=True).start()
#property
def url(self):
return self._url
#cached_property
def resolutions(self):
return list()
def _init(self):
with self._mutex:
self.resolutions.clear()
try:
self._yt = pytube.YouTube(
self.url,
on_progress_callback=self._on_progress,
on_complete_callback=self._on_complete,
)
streams = self._yt.streams.filter(mime_type="video/mp4", progressive="True")
except Exception as e:
self.initialized.emit(False, str(e))
return
with self._mutex:
self.resolutions = [stream.resolution for stream in streams]
self.initialized.emit(True, "")
def download(self, resolution, directory):
threading.Thread(
target=self._download, args=(resolution, directory), daemon=True
).start()
def _download(self, resolution, directory):
stream = self._yt.streams.get_by_resolution(resolution)
self.download_started.emit()
stream.download(directory)
def _on_progress(self, stream, chunk, bytes_remaining):
self.download_progress_changed.emit(
100 * (stream.filesize - bytes_remaining) // stream.filesize
)
def _on_complete(self, stream, filepath):
self.download_finished.emit()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.le_url = QtWidgets.QLineEdit("http://youtube.com/watch?v=2lAe1cqCOXo")
self.lbl_error = QtWidgets.QLabel()
self.btn_search = QtWidgets.QPushButton("Search")
self.cmb_resolutions = QtWidgets.QComboBox()
self.le_directory = QtWidgets.QLineEdit("")
self.btn_download = QtWidgets.QPushButton("Download")
self.pgb_download = QtWidgets.QProgressBar()
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QGridLayout(central_widget)
lay.addWidget(self.le_url, 0, 0)
lay.addWidget(self.btn_search, 0, 1)
lay.addWidget(self.cmb_resolutions, 1, 0)
lay.addWidget(self.le_directory, 1, 1)
lay.addWidget(self.btn_download, 1, 2)
lay.addWidget(self.pgb_download, 2, 0, 1, 3)
self.btn_download.setEnabled(False)
self._qpytube = None
self.btn_search.clicked.connect(self.handle_search_clicked)
self.btn_download.clicked.connect(self.handle_download_clicked)
def handle_search_clicked(self):
self.cmb_resolutions.clear()
self.btn_search.setEnabled(False)
self.btn_download.setEnabled(False)
self.lbl_error.clear()
self._qpytube = QPyTube(self.le_url.text())
self._qpytube.initialized.connect(self.handle_initialized)
self._qpytube.download_progress_changed.connect(self.pgb_download.setValue)
self._qpytube.download_started.connect(self.handle_download_started)
self._qpytube.download_finished.connect(self.handle_download_finished)
#QtCore.pyqtSlot(bool, str)
def handle_initialized(self, status, error=""):
if status:
self.cmb_resolutions.addItems(self._qpytube.resolutions)
self.btn_download.setEnabled(True)
else:
self.lbl_error.setText(error)
self.btn_search.setEnabled(True)
def handle_download_clicked(self):
self._qpytube.download(
self.cmb_resolutions.currentText(), self.le_directory.text()
)
self.btn_search.setEnabled(False)
self.btn_download.setEnabled(False)
self.le_directory.setEnabled(False)
def handle_download_started(self):
self.lbl_error.clear()
print("started")
def handle_download_finished(self):
self.pgb_download.setValue(100)
self.btn_search.setEnabled(True)
self.btn_download.setEnabled(True)
self.le_directory.setEnabled(True)
print("finished")
def main(args):
app = QtWidgets.QApplication(args)
w = MainWindow()
w.show()
app.exec_()
if __name__ == "__main__":
main(sys.argv)
I have made a Desktop Application using Python and used PyQt5 and Pytube which could download video from youtube. When download is in Progress, I want to show user an animation. In Fact I did it, but when the file is getting downloaded the PyQt window seems like freezing and everything just gets paused until the download is complete. So, Does anyone know why is this happening? How do I fix it?
Here's the code snippet:
def download_created(self, qual): # Used in 'selection' method
selected_stream = yt.streams.get_by_resolution(qual)
self.progress_func()
try:
self.download_btn.setCurrentIndex(-1)
selected_stream.download(self.askLocation() + "/")
except:
pass
# This gets the quality that the user chooses
def selection(self):
global quality
quality = self.download_btn.currentText()
try:
self.download_created(quality) # Calls a method called 'download'
except:
self.start_anime()
# Fetching the details about the Link from Youtube
def download_youtube(self):
global check
if check != self.get_input():
check = self.get_input()
self.download_btn.clear()
enter_url = self.get_input()
try:
global yt
yt = pytube.YouTube(
enter_url,
on_progress_callback = on_progress,
on_complete_callback = self.complete_func)
self.start_anime()
except:
self.input_error()
VIDEO_TITLE = (yt.title)
global VIDEO_ID
VIDEO_ID = (yt.video_id)
videos = yt.streams.filter(mime_type="video/mp4", progressive="True")
# Display all the available qualities
for i in videos:
self.download_btn.addItem(i.resolution)
self.download_btn.currentIndexChanged.connect(self.selection)
You have to execute the time consuming tasks in another thread, for example in your case the task of getting the streams and downloading.
import sys
import threading
from functools import cached_property
from PyQt5 import QtCore, QtWidgets
import pytube
class QPyTube(QtCore.QObject):
initialized = QtCore.pyqtSignal(bool, str)
download_started = QtCore.pyqtSignal()
download_progress_changed = QtCore.pyqtSignal(int)
download_finished = QtCore.pyqtSignal()
def __init__(self, url):
super().__init__()
self._url = url
self._yt = None
self._mutex = threading.Lock()
threading.Thread(target=self._init, daemon=True).start()
#property
def url(self):
return self._url
#cached_property
def resolutions(self):
return list()
def _init(self):
with self._mutex:
self.resolutions.clear()
try:
self._yt = pytube.YouTube(
self.url,
on_progress_callback=self._on_progress,
on_complete_callback=self._on_complete,
)
streams = self._yt.streams.filter(mime_type="video/mp4", progressive="True")
except Exception as e:
self.initialized.emit(False, str(e))
return
with self._mutex:
self.resolutions = [stream.resolution for stream in streams]
self.initialized.emit(True, "")
def download(self, resolution, directory):
threading.Thread(
target=self._download, args=(resolution, directory), daemon=True
).start()
def _download(self, resolution, directory):
stream = self._yt.streams.get_by_resolution(resolution)
self.download_started.emit()
stream.download(directory)
def _on_progress(self, stream, chunk, bytes_remaining):
self.download_progress_changed.emit(
100 * (stream.filesize - bytes_remaining) // stream.filesize
)
def _on_complete(self, stream, filepath):
self.download_finished.emit()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.le_url = QtWidgets.QLineEdit("http://youtube.com/watch?v=2lAe1cqCOXo")
self.lbl_error = QtWidgets.QLabel()
self.btn_search = QtWidgets.QPushButton("Search")
self.cmb_resolutions = QtWidgets.QComboBox()
self.le_directory = QtWidgets.QLineEdit("")
self.btn_download = QtWidgets.QPushButton("Download")
self.pgb_download = QtWidgets.QProgressBar()
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QGridLayout(central_widget)
lay.addWidget(self.le_url, 0, 0)
lay.addWidget(self.btn_search, 0, 1)
lay.addWidget(self.cmb_resolutions, 1, 0)
lay.addWidget(self.le_directory, 1, 1)
lay.addWidget(self.btn_download, 1, 2)
lay.addWidget(self.pgb_download, 2, 0, 1, 3)
self.btn_download.setEnabled(False)
self._qpytube = None
self.btn_search.clicked.connect(self.handle_search_clicked)
self.btn_download.clicked.connect(self.handle_download_clicked)
def handle_search_clicked(self):
self.cmb_resolutions.clear()
self.btn_search.setEnabled(False)
self.btn_download.setEnabled(False)
self.lbl_error.clear()
self._qpytube = QPyTube(self.le_url.text())
self._qpytube.initialized.connect(self.handle_initialized)
self._qpytube.download_progress_changed.connect(self.pgb_download.setValue)
self._qpytube.download_started.connect(self.handle_download_started)
self._qpytube.download_finished.connect(self.handle_download_finished)
#QtCore.pyqtSlot(bool, str)
def handle_initialized(self, status, error=""):
if status:
self.cmb_resolutions.addItems(self._qpytube.resolutions)
self.btn_download.setEnabled(True)
else:
self.lbl_error.setText(error)
self.btn_search.setEnabled(True)
def handle_download_clicked(self):
self._qpytube.download(
self.cmb_resolutions.currentText(), self.le_directory.text()
)
self.btn_search.setEnabled(False)
self.btn_download.setEnabled(False)
self.le_directory.setEnabled(False)
def handle_download_started(self):
self.lbl_error.clear()
print("started")
def handle_download_finished(self):
self.pgb_download.setValue(100)
self.btn_search.setEnabled(True)
self.btn_download.setEnabled(True)
self.le_directory.setEnabled(True)
print("finished")
def main(args):
app = QtWidgets.QApplication(args)
w = MainWindow()
w.show()
app.exec_()
if __name__ == "__main__":
main(sys.argv)
I am very new to PyQt4 and was learning how to integrate it with my Python code at the back-end. The way I did it, the front-end looks just fine and takes all the input I need. When the processing starts, that is when the back-end scripts run, the window disappears and reappears when all my back end scripts have finished. The window is supposed to show the status of the on-going process (at the back end).
Please help me with this.
A Button on Win2 makes it switch to Win4 and that is when Win4 disappears and reappears once all the back end processing is done.
Code snippet:
class Win2(QtGui.QMainWindow, w2):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.move(500,200)
self.window1 = None
self.window4 = None
self.window5 = None
self.window6 = None
self.pushButton_2.clicked.connect(self.showWin4)
self.pushButton_9.clicked.connect(self.showWin5)
self.pushButton_5.clicked.connect(self.showWin6)
self.pushButton_8.clicked.connect(self.openIP)
self.pushButton_7.clicked.connect(self.opencred)
self.pushButton_6.clicked.connect(self.showWin1)
def openIP(self):
self.filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
#print filename
self.label_7.setText(self.filename)
def from_alldevicesrun(self):
#print check_port.all_ips
with open(self.filename, "rb") as csvfile:
iprow = csv.reader(csvfile, delimiter='\n')
for [ip] in iprow:
#print ip
from_alldevices.ips.append(ip)
csvfile.close()
from_alldevices.main()
def opencred(self):
self.filename1 = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
#print filename1
self.label_9.setText(self.filename1)
def see_cred(self):
'''
import __builtin__
__builtin__.x = self.filename1
import check_credentials_both
'''
cmd = ['python', 'c.py' ]
cmd_out = subprocess.Popen( cmd , stdout=subprocess.PIPE).communicate()[0]
def showWin4(self):
self.close()
if self.window4 is None:
self.window4 = Win4()
self.window4.show()
self.window4.set_status_7('Ongoing')
self.from_alldevicesrun()
self.window4.set_status_7('Done')
self.window4.set_status_13('Ongoing')
self.see_cred()
self.window4.set_status_13('Done')
self.window4.set_status_8('Ongoing')
self.window4.get_data_from_server()
self.window4.set_status_8('Done')
self.window4.set_status_9('Ongoing')
self.window4.SSH_into_nw()
self.window4.set_status_9('Done')
self.window4.set_status_10('Ongoing')
self.window4.extract_mac()
self.window4.extract_lldp()
self.window4.port_channel_change()
self.window4.final_algo()
self.window4.set_status_10('Done')
self.window4.showWin10()
def showWin5(self):
if self.window5 is None:
self.window5 = Win5(self)
self.window5.show()
def showWin6(self):
if self.window6 is None:
self.window6 = Win6(self)
self.window6.show()
def showWin1(self):
self.close()
if self.window1 is None:
self.window1 = Win1(self)
self.window1.show()
class Win4(QtGui.QDialog, w4):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
self.move(500,200)
self.window10 = None
self.pushButton.clicked.connect(self.show_graphandWin8)
self.pushButton_2.clicked.connect(self.showWin10)
self.window8 = None
def show_graphandWin8(self):
#self.close()
if self.window8 is None:
self.window8 = Win8(self)
self.window8.show()
cmd = ['python', 'dc.py' ]
cmd_out = subprocess.Popen( cmd , stdout=subprocess.PIPE).communicate()[0]
def set_status_7(self,text):
self.label_7.setText(text)
def set_status_8(self,text):
self.label_8.setText(text)
def set_status_9(self,text):
self.label_9.setText(text)
def set_status_10(self,text):
self.label_10.setText(text)
def set_status_13(self,text):
self.label_13.setText(text)
def get_data_from_server(self):
get_data_from_servers.main()
def SSH_into_nw(self):
import SSH_and_telnet_nw
SSH_and_telnet_nw.main()
def extract_mac(self):
cmd = ['python', '123.py' ]
cmd_out = subprocess.Popen( cmd , stdout=subprocess.PIPE).communicate()[0]
def extract_lldp(self):
cmd = ['python', 'qwe.py' ]
cmd_out = subprocess.Popen( cmd , stdout=subprocess.PIPE).communicate()[0]
def port_channel_change(self):
import change_to_port_channel
change_to_port_channel.main()
def final_algo(self):
cmd = ['python', 'abc.py' ]
cmd_out = subprocess.Popen( cmd , stdout=subprocess.PIPE).communicate()[0]
def showWin10(self):
if self.window10 is None:
self.window10 = Win10(self)
self.window10.show()
def main():
app = QtGui.QApplication(sys.argv)
form = Win2()
form.show()
sys.exit(app.exec_())
Thank you in advance.
You want to use hide() not close(). When you are calling self.close() you are making the QMainWindow stop and potentially be set for deletion. With hide() it will just make the Window invisible until you want to show() it again.
def showWin4(self):
self.hide() #This should be a hide not close
if self.window4 is None:
self.window4 = Win4()
self.window4.show()
I want to display a log file in a dialogue with three buttons.I can append each line to the textBrowser with the following code, but when I try to append the whole text the GUI freezes.
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os
import sys
import time
import UI_logs
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class WorkThread(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
#Reading and appending the whole text
# logs = open('logs.txt', 'r').read()
# self.emit(QtCore.SIGNAL('update(QString)'), str(logs))
#Reading and appending line by line
logs = open('logs.txt', 'r').readlines()
for line in logs:
self.emit(QtCore.SIGNAL('update(QString)'), str(line))
time.sleep(0.1)
self.terminate()
class Logs(QtGui.QDialog):
def __init__(self, parent=None):
super(Logs, self).__init__(parent)
self.ui = UI_logs.Ui_Dialog()
self.ui.setupUi(self)
self.load_logs()
QtCore.QObject.connect(self.ui.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicked()")), self.clear_logs)
QtCore.QObject.connect(self.ui.pushButton_4, QtCore.SIGNAL(_fromUtf8("clicked()")), self.load_logs)
QtCore.QObject.connect(self.ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.export)
def display_logs(self, text):
self.ui.textBrowser.append(text)
self.ui.textBrowser.update()
self.ui.textBrowser.moveCursor(QtGui.QTextCursor.End)
def load_logs(self):
self.workThread = WorkThread()
QtCore.QObject.connect(self.workThread, QtCore.SIGNAL("update(QString)"), self.display_logs)
self.workThread.start()
def clear_logs(self):
try:
os.remove('logs.txt')
self.ui.textBrowser.clear()
info_msg = "Your logs file has been deleted."
info_reply = QtGui.QMessageBox.warning(None, 'Logs Notification', info_msg, QtGui.QMessageBox.Ok)
except:
info_msg = "Your logs file can not be deleted."
info_reply = QtGui.QMessageBox.warning(None, 'Logs Notification', info_msg, QtGui.QMessageBox.Ok)
def export(self):
log_text = self.ui.textBrowser.toPlainText()
if log_text:
filename = QtGui.QFileDialog.getSaveFileName(None, 'Export logs as',
'.', 'Text Documents (*.txt)')
if filename:
fname = open(filename, 'w')
fname.write(log_text)
fname.close()
info_msg = "Your logs file has been exported successfully."
info_reply = QtGui.QMessageBox.warning(None, 'Logs Notification', info_msg, QtGui.QMessageBox.Ok)
else:
warning_title = 'Export Warning'
warning_text = 'There is nothing in the logs to export.'
QtGui.QMessageBox.warning(None, warning_title,
warning_text, QtGui.QMessageBox.Ok)
app = QApplication(sys.argv)
app.setApplicationName('MyWindow')
window = Logs()
window.show()
sys.exit(app.exec_())
Try with one of those 3 commented lines:
logs = open('logs.txt', 'r').read()
#logs = logs.encode('utf-8')
#logs = QString(logs)
#logs = unicode(QString(logs))
self.emit(QtCore.SIGNAL('update(QString)'), str(logs))
also, try them without converting logs to string:
self.emit(QtCore.SIGNAL('update(QString)'), logs)
I don't have import UI_logs so I can't test it, but one of those should work...
I am writing a python script that will parse through a file quickly by sending lines to different processes to handle. At the end, I want the parent to receive the results from each child process and then be able to manipulate that. Here is the code:
#!/usr/bin/env python
import os
import re
from datetime import datetime
from multiprocessing import Process, JoinableQueue
class LineConsumer(Process):
def __init__(self, queue):
self.queue = queue
self.lines = 0
super(LineConsumer, self).__init__( )
def run(self):
print "My PID is %d" % self.pid
while True:
line = self.queue.get( )
print self.lines
if ':' in line:
self.lines += 1
self.queue.task_done( )
class Parser(object):
def __init__(self, filename, processes=4):
self.filename = filename
self.processes = processes
def parse(self):
queue = JoinableQueue(100)
consumers = [ ]
parents = [ ]
for i in range(0, self.processes):
lc = LineConsumer(queue)
lc.start( )
consumers.append(lc)
starttime = datetime.now( )
problem = False
numlines = 0
with open(self.filename, 'r') as data:
for line in data:
numlines += 1
def checkAlive(p):
if not p.is_alive( ):
return False
return True
alive = map(checkAlive, consumers)
if False in alive:
problem = True
print "A process died!!!"
break
queue.put(line)
if not problem:
queue.join( )
for p in consumers:
print p.lines( )
p.terminate( )
p.join( )
endtime = datetime.now( )
timedelta = endtime - starttime
lps = numlines / timedelta.total_seconds( )
print "Processed packets at %f lps" % lps
if __name__ == "__main__":
import sys
if len(sys.argv) != 2:
print "Supply a file to read"
sys.exit(1)
parser = Parser(sys.argv[1])
parser.parse( )
Here are the results:
My PID is 11578
My PID is 11579
My PID is 11580
My PID is 11581
0
1
0
2
1
3
2
1
...
555
627
564
556
628
0
0
0
0
Processed packets at 27189.771341 lps
As you can see, each child can save its line count, but when I try to access the count from the parent, I keep getting 0. How can I send the line count to the parent?
You can pass values back through an results queue.
In LineConsumer:
def __init__(self, queue, result_queue):
self.result_queue = result_queue
# ...
def terminate(self):
self.results_queue.put(self.lines)
super(LineConsumer, self).terminate()
In Parser:
queue = JoinableQueue(100)
result_queue = Queue()
# ...
lc = LineConsumer(queue, result_queue)
# ...
for p in consumers:
p.terminate()
p.join()
while True:
try:
print results.queue.get(False)
except Queue.Empty: # need to import Queue
break
I guess the issue is that you are trying to access object copy of which was actually changed in another process (child). You need to use explicit inter-process communication to send response back to parent. I.e. something like that JoinableQueue you use to pass information to children.
btw, I can't see how you send your data to consumers.