How to sharing object between multiple process thread

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 610 Views - Last Post: 06 September 2019 - 03:11 AM Rate Topic: -----

#1 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

How to sharing object between multiple process thread

Posted 03 September 2019 - 09:06 AM

I try to call multiprocessing to act my process faster.
At the moment I face some problems sharing some data with many processes from main process.
I use multiprocess.pipe() to pass the data.



1. Initialization:-

       

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

     try:
                global devices
                self.listofwritetags = self.collectwritetaglist()
                conn1, conn2 = multiprocessing.Pipe()
                devices = bussiness.initilaztion(self.comm_object,logger)
                conn1.send(devices)
                self.callmotor1dprocess = 
                multiprocessing.Process(target=callallmotor1d,args=[conn2])
                self.listofthread.append(self.callmotor1dprocess)


<!-- end snippet -->
:code:



2.Start and stop process:-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    def startprocess(self):
            for item in self.listofprocess:
                item.start()


        def stopprocess(self):
            for item in self.listofprocess:
                item.terminate()

<!-- end snippet -->
:code:

UI:-
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    self.button3 = ttk.Button(self.frame, text='Start', command=self.startprocess,state =NORMAL)
            self.button3.grid(column=1, row=4, sticky=W,padx=5, pady=5)

            self.button4 = ttk.Button(self.frame, text='Stop', command=self.stopprocess, state=NORMAL)
            self.button4.grid(column=1, row=5, sticky=W, padx=5, pady=5)


<!-- end snippet -->
:code:

I need to call avobe 3 process in one class:- FormUI

but I call executable process outside of the FormUI class.

I got an error when I try to initialize.I want to pass devices from main process to child process.

The calling method:-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    def callallmotor1d(conn):
        devices = conn.recv()
        bussiness.motorallprocessing(devices)



    def callallsov1s(conn):
        devices = conn.recv()
        bussiness.motorallprocessing(devices)

<!-- end snippet -->
:code:



Any help regarding this would be appreciable


Error messege:-

2019-09-03 13:15:33,755: can't pickle _thread.lock objects
Traceback (most recent call last):
File "C:/Users/misu01/PycharmProjects/opcua/ui_v7.py", line 226, in initilization
conn1.send(devices)
File "C:\Users\misu01\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "C:\Users\misu01\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects

This post has been edited by modi123_1: 03 September 2019 - 09:12 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: How to sharing object between multiple process thread

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,193
  • Joined: 12-June 08

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 09:13 AM

Also seen here:
https://stackoverflo...cess-using-pipe
https://stackoverflo...ad-using-daemon

https://python-forum...cess-using-Pipe
Was This Post Helpful? 1
  • +
  • -

#3 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 09:22 AM

View Postmodi123_1, on 03 September 2019 - 09:13 AM, said:




Hello, Thank you for your support.
Any help reagarding this.Those post are empty

Hello,
The link which you shared with us,there is no remark or answare.
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,745
  • Joined: 27-December 13

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 09:25 AM

I think the snippets are way too fragmented to get an overview of your code. This is probably the reason behind the low response to your posts.
Was This Post Helpful? 0
  • +
  • -

#5 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 09:29 AM

Hello,

Thank you for your response.

How to improve my question
Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,745
  • Joined: 27-December 13

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 09:45 AM

Show more code.
Explain what you expect the different code parts to do.
Show the test data producing the error.
Explain what output you expected from the test data.
Was This Post Helpful? 0
  • +
  • -

#7 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 09:59 AM

This is my FormUi class

class FormUi:

    def __init__(self, frame):
        self.frame = frame
        self.listofwritetags = []

        self.button1 = ttk.Button(self.frame, text='Connect', command=self.connectwithplc)
        self.button1.grid(column=1, row=2, sticky=W,padx=5, pady=5)
        self.button2 = ttk.Button(self.frame, text='Initialization', command=self.initilization,state=DISABLED)
        self.button2.grid(column=1, row=3, sticky=W,padx=5, pady=5)

        self.button3 = ttk.Button(self.frame, text='Start', command=self.startprocess,state =DISABLED)
        self.button3.grid(column=1, row=4, sticky=W,padx=5, pady=5)

        self.button4 = ttk.Button(self.frame, text='Stop', command=self.stopprocess, state=NORMAL)
        self.button4.grid(column=1, row=5, sticky=W, padx=5, pady=5)

        self.button5 = ttk.Button(self.frame, text='Force', command=self.force, state=DISABLED)
        self.button5.grid(column=1, row=6, sticky=W, padx=5, pady=5)



    def connectwithplc(self):

        try:
            self.comm_object = general.General()
            self._elementlist = []
            self.tagvalueitemlist = []
            self.plc_cmd_list = []
            self.readsuccess = False
            self.eventtag = 'C1_LANCE2_LIFE'

        except Exception as e:
            level = logging.ERROR
            now = datetime.datetime.now()
            messege = "Error is " +str(e) + "from communication function"
            logger.log(level,messege)
            log_exception(e)

        finally:
            self.comm_sts = self.comm_object.sta_con_plc
            if self.comm_sts:
                self.button1.config(text="Connected")
                self.button2["state"] = NORMAL
                level = logging.INFO
                messege =  'Event:' + "PLC Simulation Successfully Connected"
                logger.log(level, messege)



    def initilization(self):
        initial = "False"

        self.listofthread =[]
        self.listofprocess = []

        try:
            global devices
            self.listofwritetags = self.collectwritetaglist()
            conn1, conn2 = multiprocessing.Pipe()
            devices = bussiness.initilaztion(self.comm_object,logger)
            conn1.send(devices)
            self.callmotor1dprocess = multiprocessing.Process(target=callallmotor1d,args=[conn2])
            self.listofthread.append(self.callmotor1dprocess)
            self.callsov1sprocess =  multiprocessing.Process(target=callallsov1s,args=[conn2])
            self.listofthread.append(self.callsov1sprocess)

            self.button2.config(text="Initialized")
            initial = "True"

        except Exception as e:
            initial = "False"
            level = logging.INFO
            messege = 'Event:' + "Initialization Failed"
            logger.log(level, messege)
            logger.exception(e)

        finally:
            if initial:
                self.button3["state"] = NORMAL




    def startprocess(self):
        for item in self.listofthread:
            item.start()
        self.button3.config(text="started")



    def stopprocess(self):
        for item in self.listofthread:
            item.kill()

    def force(self):
        win = tk.Toplevel(self.frame)
        win.geometry('300x100')
        ttk.Label(win,text = 'Choose Tag').grid(column=1,row = 0)
        tagname_entered = AutocompleteCombox.AutocompleteCombobox(win,width=20)
        tagname_entered.grid(column=1,row = 1)
        tagname_entered.set_completion_list(self.listofwritetags)
        tagname_entered.focus_set()
        ttk.Label(win, text='Enter Value').grid(column=2, row=0)
        value = IntVar()
        tagvalue =ttk.Entry(win,width=12,textvariable = value)
        tagvalue.grid(column=2, row=1)
        ttk.Label(win, text='Action').grid(column=2, row=3)
        action = ttk.Button(win,text = "Submit",command = self.writetag(tagname_entered,tagvalue))
        win.bind('<Control-Q>', lambda event=None: win.destroy())
        win.bind('<Control-q>', lambda event=None: win.destroy())

    def collectwritetaglist(self):
        list1 = []
        df = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='WriteGeneral')
        n = 0
        while n < len(df.index):
            list1.append(n)
            n = n + 1
        return  list1

    def writetag(self,tagname_entered,tagvalue):
        tagname = str(tagname_entered.get())
        tagvalue = int(tagvalue.get())
        try:
            if len(tagname) > 3:
                self.comm_object.writegeneral.writenodevalue(tagname, tagvalue)
                level = logging.DEBUG
                messege = tagname + " Force Value is " + str(tagvalue)
                logger.log(level, messege)

        except Exception as e :
            log_exception(e)
            level = logging.ERROR
            messege = "Error is " +str(e) + "from Force function"
            logger.log(level, messege)





Now If I press start button from UI, it should start my multiprocessing thread.

I call two function outside of class becasue Multiprocessing doesn't take any self object.

def callallmotor1d(conn):
    devices = conn.recv()
    bussiness.motorallprocessing(devices)



def callallsov1s(conn):
    devices = conn.recv()
    bussiness.motorallprocessing(devices)





Now I have to pass device object from main process to it's child process

I take help of multiprocessing.Pipe() to do the same but not successed.

I got an error:-

019-09-03 13:15:33,755: can't pickle _thread.lock objects
Traceback (most recent call last):
File "C:/Users/misu01/PycharmProjects/opcua/ui_v7.py", line 226, in initilization
conn1.send(devices)
File "C:\Users\misu01\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "C:\Users\misu01\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
TypeError: can't pickle _thread.lock objects


So, Any help regarding this appreciable.

Thank you
Was This Post Helpful? 0
  • +
  • -

#8 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,745
  • Joined: 27-December 13

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 10:16 AM

Come-on Bubai2019, we are not magicians and we cannot guess what's in your main code.

Do you press the two buttons 'connect' and 'initialization' ? What happens, you you see it working successfully? What is in the 'self.listofthreads'?

Besides: The two small helper-functions are exactly the same.
Was This Post Helpful? 0
  • +
  • -

#9 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 10:26 AM

Yes connect and initilize button are functioned okye.
Error I got when I press start button
Was This Post Helpful? 0
  • +
  • -

#10 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 10:38 AM

listof thread , adding all multiporess thread together and start my thread when I press start button.

Actully error I got due Multiprocessing.

Multiprocessing.process can't work well in class, I don't know why.


As my project demands to use Multiprocess and pass class object.

Thus why I declear device as global not as self.device but still showing error

def callallmotor1d(conn):
    devices = conn.recv()
    bussiness.motorallprocessing(devices)



def callallsov1s(conn):
    devices = conn.recv()
    bussiness.allsov1processing(devices)




This two are continuous processing which are responsible to read and write huge amount of data into plc.


def callallmotor1d(conn):
devices = conn.recv()
while True:
bussiness.motorallprocessing(devices)




def callallsov1s(conn):
while True:
devices = conn.recv()
bussiness.allsov1processing(devices)

more modified version.

Previous I use multithread but in case of CPU IO bound Multiprocess is more faster.
Was This Post Helpful? 0
  • +
  • -

#11 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 10:51 AM

bye the way thank you for supporting me,

I pasted here my full code :-

import datetime
import queue
import logging
import signal
from tkinter import *
from PIL import ImageTk
from multiprocessing import Manager, Queue,Pool
import PIL.Image
from tkinter import filedialog
import bussiness
import multiprocessing
import AutocompleteCombox
import general
import time
from ListView2 import *
from logger import *
import alldevices
from clientcomm_v1 import *
import threading

from tkinter.scrolledtext import ScrolledText
from tkinter import ttk, VERTICAL, HORIZONTAL, N, S, E, W


logger = logging.getLogger(__name__)



def function2():
    while True:
        i = 0
        for i in range(10):
            print("Hi 2nd loop")


class Clock(threading.Thread):
    """Class to display the time every seconds
    Every 5 seconds, the time is displayed using the logging.ERROR level
    to show that different colors are associated to the log levels
    """

    def __init__(self):
        super().__init__()
        self._stop_event = threading.Event()

    def run(self):
        logger.debug('Simulation started')
        while not self._stop_event.is_set():
            now = datetime.datetime.now()
            # level = logging.INFO
            # logger.log(level, now)
            time.sleep(1)

    def stop(self):
        self._stop_event.set()


class thread_with_trace(threading.Thread):
    def __init__(self, *args, **keywords):
        threading.Thread.__init__(self, *args, **keywords)
        self.killed = False

    def start(self):
        self.__run_backup = self.run
        self.run = self.__run
        threading.Thread.start(self)

    def __run(self):
        sys.settrace(self.globaltrace)
        self.__run_backup()
        self.run = self.__run_backup

    def globaltrace(self, frame, event, arg):
        if event == 'call':
            return self.localtrace
        else:
            return None

    def localtrace(self, frame, event, arg):
        if self.killed:
            if event == 'line':
                raise SystemExit()
        return self.localtrace

    def kill(self):
        self.killed = True

class process_with_trace(multiprocessing.Process):
    def __init__(self, *args, **keywords):
        multiprocessing.Process.__init__(self, *args, **keywords)
        self.killed = False

        self.queue = multiprocessing.Queue()


    def run(self):
        self.process = multiprocessing.Process.start(self)

    def kill(self):
        self.process.terminate()


class QueueHandler(logging.Handler):
    """Class to send logging records to a queue
    It can be used from different threads
    The ConsoleUi class polls this queue to display records in a ScrolledText widget
    """
    # Example from Moshe Kaplan: https://gist.github.com/moshekaplan/c425f861de7bbf28ef06
    # (https://stackoverflow.com/questions/13318742/python-logging-to-tkinter-text-widget) is not thread safe!
    # See https://stackoverflow.com/questions/43909849/tkinter-python-crashes-on-new-thread-trying-to-log-on-main-thread

    def __init__(self, log_queue):
        super().__init__()
        self.log_queue = log_queue

    def emit(self, record):
        self.log_queue.put(record)


class ConsoleUi:
    """Poll messages from a logging queue and display them in a scrolled text widget"""

    def __init__(self, frame):
        self.frame = frame
        # Create a ScrolledText wdiget
        self.scrolled_text = ScrolledText(frame, state='disabled', height=12)
        self.scrolled_text.grid(row=0, column=0, sticky=(N, S, W, E))
        self.scrolled_text.configure(font='TkFixedFont')
        self.scrolled_text.tag_config('INFO', foreground='black')
        self.scrolled_text.tag_config('DEBUG', foreground='gray')
        self.scrolled_text.tag_config('WARNING', foreground='orange')
        self.scrolled_text.tag_config('ERROR', foreground='red')
        self.scrolled_text.tag_config('CRITICAL', foreground='red', underline=1)
        # Create a logging handler using a queue
        self.log_queue = queue.Queue()
        self.queue_handler = QueueHandler(self.log_queue)
        formatter = logging.Formatter('%(asctime)s: %(message)s')
        self.queue_handler.setFormatter(formatter)
        logger.addHandler(self.queue_handler)
        # Start polling messages from the queue
        self.frame.after(100, self.poll_log_queue)

    def display(self, record):
        msg = self.queue_handler.format(record)
        self.scrolled_text.configure(state='normal')
        self.scrolled_text.insert(tk.END, msg + '\n', record.levelname)
        self.scrolled_text.configure(state='disabled')
        # Autoscroll to the bottom
        self.scrolled_text.yview(tk.END)

    def poll_log_queue(self):
        # Check every 100ms if there is a new message in the queue to display
        while True:
            try:
                record = self.log_queue.get(block=False)
            except queue.Empty:
                break
            else:
                self.display(record)
        self.frame.after(100, self.poll_log_queue)


class FormUi:

    def __init__(self, frame):
        self.frame = frame
        self.listofwritetags = []

        self.button1 = ttk.Button(self.frame, text='Connect', command=self.connectwithplc)
        self.button1.grid(column=1, row=2, sticky=W,padx=5, pady=5)
        self.button2 = ttk.Button(self.frame, text='Initialization', command=self.initilization,state=DISABLED)
        self.button2.grid(column=1, row=3, sticky=W,padx=5, pady=5)

        self.button3 = ttk.Button(self.frame, text='Start', command=self.startprocess,state =DISABLED)
        self.button3.grid(column=1, row=4, sticky=W,padx=5, pady=5)

        self.button4 = ttk.Button(self.frame, text='Stop', command=self.stopprocess, state=NORMAL)
        self.button4.grid(column=1, row=5, sticky=W, padx=5, pady=5)

        self.button5 = ttk.Button(self.frame, text='Force', command=self.force, state=DISABLED)
        self.button5.grid(column=1, row=6, sticky=W, padx=5, pady=5)



    def connectwithplc(self):

        try:
            self.comm_object = general.General()
            self._elementlist = []
            self.tagvalueitemlist = []
            self.plc_cmd_list = []
            self.readsuccess = False
            self.eventtag = 'C1_LANCE2_LIFE'

        except Exception as e:
            level = logging.ERROR
            now = datetime.datetime.now()
            messege = "Error is " +str(e) + "from communication function"
            logger.log(level,messege)
            log_exception(e)

        finally:
            self.comm_sts = self.comm_object.sta_con_plc
            if self.comm_sts:
                self.button1.config(text="Connected")
                self.button2["state"] = NORMAL
                level = logging.INFO
                messege =  'Event:' + "PLC Simulation Successfully Connected"
                logger.log(level, messege)



    def initilization(self):
        initial = "False"

        self.listofthread =[]
        self.listofprocess = []

        try:
            global devices
            self.listofwritetags = self.collectwritetaglist()
            conn1, conn2 = multiprocessing.Pipe()
            devices = bussiness.initilaztion(self.comm_object,logger)
            conn1.send(devices)
            self.callmotor1dprocess = multiprocessing.Process(target=callallmotor1d,args=[conn2])
            self.listofthread.append(self.callmotor1dprocess)
            self.callsov1sprocess =  multiprocessing.Process(target=callallsov1s,args=[conn2])
            self.listofthread.append(self.callsov1sprocess)

            self.button2.config(text="Initialized")
            initial = "True"

        except Exception as e:
            initial = "False"
            level = logging.INFO
            messege = 'Event:' + "Initialization Failed"
            logger.log(level, messege)
            logger.exception(e)

        finally:
            if initial:
                self.button3["state"] = NORMAL




    def startprocess(self):
        for item in self.listofthread:
            item.start()
        self.button3.config(text="started")



    def stopprocess(self):
        for item in self.listofthread:
            item.kill()

    def force(self):
        win = tk.Toplevel(self.frame)
        win.geometry('300x100')
        ttk.Label(win,text = 'Choose Tag').grid(column=1,row = 0)
        tagname_entered = AutocompleteCombox.AutocompleteCombobox(win,width=20)
        tagname_entered.grid(column=1,row = 1)
        tagname_entered.set_completion_list(self.listofwritetags)
        tagname_entered.focus_set()
        ttk.Label(win, text='Enter Value').grid(column=2, row=0)
        value = IntVar()
        tagvalue =ttk.Entry(win,width=12,textvariable = value)
        tagvalue.grid(column=2, row=1)
        ttk.Label(win, text='Action').grid(column=2, row=3)
        action = ttk.Button(win,text = "Submit",command = self.writetag(tagname_entered,tagvalue))
        win.bind('<Control-Q>', lambda event=None: win.destroy())
        win.bind('<Control-q>', lambda event=None: win.destroy())

    def collectwritetaglist(self):
        list1 = []
        df = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='WriteGeneral')
        n = 0
        while n < len(df.index):
            list1.append(n)
            n = n + 1
        return  list1

    def writetag(self,tagname_entered,tagvalue):
        tagname = str(tagname_entered.get())
        tagvalue = int(tagvalue.get())
        try:
            if len(tagname) > 3:
                self.comm_object.writegeneral.writenodevalue(tagname, tagvalue)
                level = logging.DEBUG
                messege = tagname + " Force Value is " + str(tagvalue)
                logger.log(level, messege)

        except Exception as e :
            log_exception(e)
            level = logging.ERROR
            messege = "Error is " +str(e) + "from Force function"
            logger.log(level, messege)






















class ThirdUi:

    def __init__(self,frame):
        self.frame = frame
        stim_filename = "smslogo.png"
        # create the PIL image object:
        PIL_image = PIL.Image.open(stim_filename)
        self.img = ImageTk.PhotoImage(file="smslogo.png")
        ttk.Label(self.frame, text='Progress Bar').pack(side=LEFT)
        self.progress = ttk.Progressbar(self.frame, orient=HORIZONTAL, length=500, mode='indeterminate')
        label=Label(self.frame, image =self.img)
        label.image = self.img
        Label.anchor=N
        self.progress.pack(side = LEFT)
        label.pack(side='right')





        # L = ttk.Label(self.frame, image=img)






class App:

    def __init__(self, root):
        self.root = root
        root.title('SMS SIMULATION')
        root.columnconfigure(0, weight=1)
        root.rowconfigure(0, weight=1)
        # Create the panes and frames
        vertical_pane = ttk.PanedWindow(self.root, orient=VERTICAL)
        vertical_pane.grid(row=0, column=0, sticky="nsew")
        horizontal_pane = ttk.PanedWindow(vertical_pane, orient=HORIZONTAL)
        vertical_pane.add(horizontal_pane)
        form_frame = ttk.Labelframe(horizontal_pane, text="Control Panel")
        form_frame.columnconfigure(1, weight=1)
        horizontal_pane.add(form_frame, weight=1)
        console_frame = ttk.Labelframe(horizontal_pane, text="Console")
        console_frame.columnconfigure(0, weight=1)
        console_frame.rowconfigure(0, weight=1)
        horizontal_pane.add(console_frame, weight=1)
        third_frame = LabelFrame(vertical_pane, text="Third Panel")
        vertical_pane.add(third_frame, weight=1)
        # Initialize all frames
        self.form = FormUi(form_frame)
        self.console = ConsoleUi(console_frame)
        self.third = ThirdUi(third_frame)
        self.clock = Clock()
        self.clock.start()
        self.root.protocol('WM_DELETE_WINDOW', self.quit)
        self.root.bind('<Control-q>', self.quit)
        signal.signal(signal.SIGINT, self.quit)

    def quit(self, *args):
        self.clock.stop()
        self.root.destroy()


def callallmotor1d(conn):
    devices = conn.recv()
    while True:
        bussiness.motorallprocessing(devices)




def callallsov1s(conn):
    while True:
        devices = conn.recv()
        bussiness.allsov1processing(devices)





def main():
    logging.basicConfig(level=logging.DEBUG)
    df = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Tag List')
    root = tk.Tk()
    app = App(root)
    app.root.mainloop()


if __name__ == '__main__':
    main()




Was This Post Helpful? 0
  • +
  • -

#12 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 03 September 2019 - 11:25 PM

Hello Sir,
Good Morning!!

Any suggestion ?
Was This Post Helpful? 0
  • +
  • -

#13 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 557
  • View blog
  • Posts: 1,745
  • Joined: 27-December 13

Re: How to sharing object between multiple process thread

Posted 05 September 2019 - 05:31 AM

I'm not sure I can help much - too many modules that I'm unfamiliar with.

In post #9 you write that the button 'initilization' works ok (correct spelling is 'initialization').

But the error reported in post #1 is

Quote

Traceback (most recent call last):
File "C:/Users/misu01/PycharmProjects/opcua/ui_v7.py", line 226, in initilization
conn1.send(devices)
This indicates a problem with this part, not the 'startproces()'

This post has been edited by DK3250: 05 September 2019 - 05:33 AM

Was This Post Helpful? 0
  • +
  • -

#14 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 05 September 2019 - 08:49 PM

View PostDK3250, on 05 September 2019 - 05:31 AM, said:

I'm not sure I can help much - too many modules that I'm unfamiliar with.

In post #9 you write that the button 'initilization' works ok (correct spelling is 'initialization').

But the error reported in post #1 is

Quote

Traceback (most recent call last):
File "C:/Users/misu01/PycharmProjects/opcua/ui_v7.py", line 226, in initilization
conn1.send(devices)
This indicates a problem with this part, not the 'startproces()'


Thank you for your concern.I try to fix this problem.This error occured because of thread.lock is not pickable.
pickling class instance is bit more difficult.

Here is the post:- https://docs.python....class-instances
Try to understand this an impliment this but not successed yet.

If you are interested on this issue please let me know.
I will open my github project.
Was This Post Helpful? 0
  • +
  • -

#15 Bubai2019   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-September 19

Re: How to sharing object between multiple process thread

Posted 06 September 2019 - 03:02 AM

Hello,

I try to remove thread.lock which is unpickable object and create again

class AllDevices:

    def __init__(self,comobject,logger):
        self.mylock = threading.Lock()

        self._comobject = comobject
        dfM1D = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Motor1D')
        dfM2D = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Motor2D')
        dfsov1S = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Valve1S')
        dfsov2S = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Valve2S')
        dfanalog = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='AnalogTx')
        dfcontrolvalve = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='ControlValves')
        dfvibrofeeder = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='VibroFeeder')
        dfconveyor = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Conveyor')
        dfdrives = pd.read_excel(r'D:\OPCUA\Working_VF1.xls', sheet_name='Drive')

        self.allmotor1dobjects = callallmotor1D_V1.Cal_AllMotor1D(dfM1D, comobject, logger)
        self.allmotor2dobjects =  callallmotor2D_V1.Cal_AllMotor2D(dfM2D,comobject,logger)
        self.allsov1sobjects = callallsov1S_V1.Cal_AllSov1S(dfsov1S,comobject,logger)
        self.allsov2sobjects = callallsov2S_V1.Cal_AllSov2S(dfsov2S, comobject,logger)
        self.allanalogobjects = calallanalog_V1.Cal_AllAnalogInputs(dfanalog,comobject,logger)
        self.allcontrolvalveobjects = calallcontrolvalves_V1.Cal_AllControlValve(dfcontrolvalve,comobject,logger)
        self.allvibrofeederobjects = callallvibrofeeder_V1.Cal_AllVibroFeeder(dfvibrofeeder,comobject,logger)
        self.allconveyorobjects = callallconveyor_V1.Cal_AllConveyor1D(dfconveyor,comobject,logger)
        self.allabbdrivesobjects = calallABPdrives_V1.Cal_AllDrives(dfdrives, comobject,logger)

        # copyreg.pickle(AllDevices,pickle_alldevice)

    def __getstate__(self):
        # Copy the object's state from self.__dict__ which contains
        # all our instance attributes. Always use the dict.copy()
        # method to avoid modifying the original state.
        state = vars(self).copy()
        # Remove the unpicklable entries.
        del state['mylock']
        return state

    def __setstate__(self, state):
        # Restore instance attributes.
        vars(self).update(state)


    def __deepcopy__(self, memo):
        newself = self.__class__.__new__(self.__class__)
        for name, value in vars(self).items():
            if name != 'mylock':
                value = copy.deepcopy(value)
            setattr(newself, name, value)
        return newself





    @property
    def allmotor1d(self):
        return self.allmotor1dobjects

    @property
    def allsov1s(self):
        return self.allsov1sobjects






but still get same error.

Still I got the same error.

2019-09-06 10:54:17,835: can't pickle _thread.lock objects Traceback (most recent call last): File "C:\Users\misu01\Desktop\SMS_SIMULATION\ui_v7.py", line 220, in initilization conn1.send(devices) File "C:\Users\misu01\AppData\Local\Programs\Python\Python37\lib\multiprocessing\connection.py", line 206, in send self._send_bytes(_ForkingPickler.dumps(obj)) File "C:\Users\misu01\AppData\Local\Programs\Python\Python37\lib\multiprocessing\reduction.py", line 51, in dumps cls(buf, protocol).dump(obj) TypeError: can't pickle _thread.lock objects

Is that my implementation is correct?

If my alldevices class contains also contains instance of classes, that could be also caused the problem.Like self.allmotor1dobjects,self.allsov1sobjects.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2