有什么方法可以在Python中的多个过程之间传递过程对象?

发布于 2025-02-07 22:53:28 字数 1109 浏览 1 评论 0原文

我在同一目录中有两个文件, 一个模块具有持续运行的函数,无法停止或暂停之间,

# script.py
def run():
    i = 0
    while True:
        print('\r', i, '\r', end='')
        i += 1
    

然后有一个自定义的调试器模块,如果目录已更新,

# debugger.py
import os
import wait
from multiprocessing import Process, Queue

def capture_change(processesQueue):
    prev = os.path.getsize('wait.py')
    while True:
        curr = os.path.getsize('wait.py')

        if curr != prev:
            print('Changes Detected!!')
            process = processesQueue.get()
            process.kill()
            process.start()
            processesQueue.put(process)
        
        prev = curr

if __name__ == "__main__":
    processesQueue = Queue()
    p1 = Process(target= wait.run)
    processesQueue.put(p1)
    p2 = Process(target= capture_change, args= (processesQueue, ))

    p1.start()
    p2.start()

我会捕获此错误,

    raise TypeError(
TypeError: Pickling an AuthenticationString object is disallowed for security reasons

我该怎么办? 我已经搜索了很多,但是在其他过程中进行数据共享的所有方法都不允许在其中共享过程实例本身。还有其他方法吗?

I have two files in the same directory,
A module has a function that is continously running and has no way to stop or pause in between,

# script.py
def run():
    i = 0
    while True:
        print('\r', i, '\r', end='')
        i += 1
    

Then there is a custom made debugger module which captures if the directory has been updated,

# debugger.py
import os
import wait
from multiprocessing import Process, Queue

def capture_change(processesQueue):
    prev = os.path.getsize('wait.py')
    while True:
        curr = os.path.getsize('wait.py')

        if curr != prev:
            print('Changes Detected!!')
            process = processesQueue.get()
            process.kill()
            process.start()
            processesQueue.put(process)
        
        prev = curr

if __name__ == "__main__":
    processesQueue = Queue()
    p1 = Process(target= wait.run)
    processesQueue.put(p1)
    p2 = Process(target= capture_change, args= (processesQueue, ))

    p1.start()
    p2.start()

I am getting this error,

    raise TypeError(
TypeError: Pickling an AuthenticationString object is disallowed for security reasons

What should I do?
I have searched alot but all the ways for data sharing among other processes are not allowing Process instance itself to be shared among them. Is there any other way around??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余罪 2025-02-14 22:53:28

多亏了@flakes,我终于在希望达到的部分方面得到了解决,

# debugger.py
import os
from multiprocessing import Process


def capture_change(processesQueue):
    prev = os.path.getsize('wait.py')
    import wait
    process = Process(target= wait.run)
    process.start()
    while True:
        curr = os.path.getsize('wait.py')

        if curr != prev:
            import wait
            print('Changes Detected!!')
            if process.is_alive():
                process.kill()
            process = Process(target= wait.run)
            process.start()
        
        prev = curr

if __name__ == "__main__":
    capture_change_and_reload()

这是我的最终代码,可根据需要运行。

Thanks to @flakes I have finally got a way around the part I was hoping to achieve,

# debugger.py
import os
from multiprocessing import Process


def capture_change(processesQueue):
    prev = os.path.getsize('wait.py')
    import wait
    process = Process(target= wait.run)
    process.start()
    while True:
        curr = os.path.getsize('wait.py')

        if curr != prev:
            import wait
            print('Changes Detected!!')
            if process.is_alive():
                process.kill()
            process = Process(target= wait.run)
            process.start()
        
        prev = curr

if __name__ == "__main__":
    capture_change_and_reload()

This is my final code that works as required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文