Python多处理 - 在进程重启后清除RAM

时间:2017-01-07 12:12:09

标签: python multiprocessing

我正在使用python多处理库来实时处理大数据集。

有时我需要重新启动更改某些设置的过程。

是否有可能清除保存在RAM中的数据。我看到我的RAM使用率正在增长〜2%/重启。

请查看以下代码示例:

# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import multiprocessing
import sys

def calc(q):
    x=5
    for i in range (10):
        x = x+1
        q.put(x)

def calc_calc(quee, qq):
    for u in range (10):
        y = quee.get()
        z = y*10    
        qq.put(z)

def calc_end(queee, qqq):
    for w in range (10):
        h = queee.get()
        xy = h*10
        print(xy)
        qqq.put(xy)

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):   
        p1 = multiprocessing.Process(target=calc, args=(q,))
        #q = q.put(x)
        p2 = multiprocessing.Process(target=calc_calc, args =(q, qq,))
        #qq = qq.put(z)
        p3 = multiprocessing.Process(target= calc_end, args =(qq, qqq,))

        p1.start()
        p2.start()
        p3.start()    

        p1.join()
        p2.join()
        p3.join()    

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Window()

    #multiprocessing queue 1
    q = multiprocessing.Queue()
    #multiprocessing queue 3
    qq = multiprocessing.Queue()
    #multiprocessing queue 3 (aktuell nicht belegt)
    qqq = multiprocessing.Queue()

    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

您无法明确释放内存。您可以做的是根据Python Documentation使用/*this is my java code which when i compile they don not show exactly error */ public class Login { public JFrame frame; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Login window = new Login(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } Connection conections; private JTextField textField; private JPasswordField passwordField; public Login() { initialize(); conections = SqliteConnectio.dbConnection(); } private void initialize() { frame = new JFrame(); frame.setBounds(100, 100, 581, 411); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); textField = new JTextField(); textField.setBounds(266, 112, 193, 45); frame.getContentPane().add(textField); textField.setColumns(10); JButton btnLOGIN = new JButton("LOGIN"); Image jn=new ImageIcon(this.getClass().getResource("/Ok.png")).getImage(); btnLOGIN.setIcon(new ImageIcon(jn)); btnLOGIN.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { String query = "select *from customer.sqlite where username=? and password=? "; PreparedStatement pre = conections.prepareStatement(query); pre.setString(1, textField.getText()); pre.setString(2, passwordField.getText()); ResultSet res = pre.executeQuery(); int count = 0; while (res.next()) { count++; } if (count == 1) { JOptionPane.showInternalMessageDialog(null, "username and password has sucseeful inserted"); } else if (count > 1) {JOptionPane.showInternalMessageDialog(null, "username and password has sucseeful inserted"); } else { JOptionPane.showInternalMessageDialog(null, "username and password has been failed to be entered"); } res.close(); pre.close(); } catch (Exception e) { JOptionPane.showInternalMessageDialog(null, "you have failed to asses the database"); } } }); btnLOGIN.setBounds(321, 256, 125, 36); frame.getContentPane().add(btnLOGIN); JLabel userNAME = new JLabel("USERNAME"); userNAME.setBounds(156, 127, 85, 14); frame.getContentPane().add(userNAME); JLabel passLABEL = new JLabel("PASSWORD"); passLABEL.setBounds(156, 191, 85, 14); frame.getContentPane().add(passLABEL); passwordField = new JPasswordField(); passwordField.setBounds(266, 188, 193, 20); frame.getContentPane().add(passwordField); JLabel lblNewLabel = new JLabel("New label"); Image im=new ImageIcon(this.getClass().getResource("/login.png")).getImage(); lblNewLabel.setIcon(new ImageIcon(im)); lblNewLabel.setBounds(10, 21, 136, 205); frame.getContentPane().add(lblNewLabel); } } 调用垃圾收集器。