'TypeError: cannot pickle '_thread.lock' object , EOFError: Ran out of input

Trying to use multiprocessing for running a watchdog for folder as a seperate process. The watchdog should start running as a seperate process so that other functions will begin to run and populate the folder the watchdog is wathcing and wtachdog will take some actions on them.

Code for watchdog :

import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import multiprocessing as mp
from multiprocessing import Pool

import os
from dotenv import load_dotenv

class Watcher:
    DIRECTORY_TO_WATCH = "C:\\Users\\Shreyas24\Desktop\\automate\\ILT"

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(1)
        except:
            self.observer.stop()
            print("Error")

        self.observer.join()


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):

        if event.event_type == 'created':
            # Take any action here when a file is first created.
            print("Received created event")

        elif event.event_type == 'deleted':
            # Taken any action here when a file is modified.
            print("Received modified event")

Code for my main file :

import os
from dotenv import load_dotenv
from automate.utils.monitor import Watcher
import multiprocessing as mp

def run():
    
    load_dotenv()                                                                         
    watch = Watcher()

    p1 = mp.Process(target=watch.run)
    
    # p1.daemon=True

    p1.start()

Error log :

Traceback (most recent call last):
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\Shreyas24\Desktop\automate\__main__.py", line 4, in <module>
    app.run()
  File "C:\Users\Shreyas24\Desktop\automate\app.py", line 22, in run
    p1.start()
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_thread.lock' object
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
PS C:\Users\Shreyas24\Desktop> python -m automate
Traceback (most recent call last):
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\Shreyas24\Desktop\automate\__main__.py", line 4, in <module>
    app.run()
  File "C:\Users\Shreyas24\Desktop\automate\app.py", line 22, in run
    p1.start()
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_thread.lock' object
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\Shreyas24\AppData\Local\Programs\Python\Python310\lib\multiprocessing\spawn.py", line 126, in _main
    self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
    
        


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source