如何在主程序退出后仍能并行运行函数?

3

简述

如何使用multiprocessing库或其他方式,使并行进程独立于主进程运行?(目前的示例代码使用fork)

背景信息

我正在为我的conky配置编写一个小型后端脚本,其中之一是rss_parser.py,它会获取一组标题/URL并将其放在stdout中。最近,我想到一个使用GTKNotify来激活新通知的方法,当RSS列表中出现新内容时。

但我有一些问题。我设置了一个打开链接的操作,因此当我的通知出现时,我可以单击并打开浏览器。代码的这部分需要等待用户点击或通知关闭才能完成,因此需要在适当的事件循环中运行。然而,这与另一件事冲突:为了更新我的conky,我需要让我的主要脚本在不等待通知部分的情况下结束!所以我的想法是将通知并行处理。

代码

我尝试过使用这个代码:(请检查parallel_notify函数)。

#!/usr/bin/env python
# coding=utf-8
#
#   Python Script
#
#   Copyleft © Manoel Vilela
#
#

import feedparser
from argparse import ArgumentParser
from string import ascii_letters as alphabet
from os import fork
import json
import sys
import webbrowser

import gi
gi.require_version('Notify', '0.7')
from gi.repository import GObject  # noqa
from gi.repository import Notify   # noqa


class RssNotifier(GObject.Object):
    Notify.init("rss_conky")
    notifications = []

    def __init__(self, label):
        self.label = label
        self.loop = GObject.MainLoop()
        super(RssNotifier, self).__init__()
        GObject.timeout_add(100, self.exit_when_empty)
        # lets initialise with the application name

    def send_notification(self, title, text, url, file_path_to_icon=""):

        n = Notify.Notification.new(title, text, file_path_to_icon)
        # print('put notification')
        self.notifications.append(n)
        n.add_action(url, 'open', self.open_webbrowser)
        n.connect('closed', self.close_notification, n)
        n.show()

    def send_rss(self, rss, url):
        self.send_notification(self.label, rss, url, 'rss')

    def open_webbrowser(self, n, url):
        # print(':: webbrowse opening')
        webbrowser.open(url)

    def close_notification(self, n, arg):
        self.notifications.remove(n)
        # print(':: remove notification')
        # print(':: notifications: ', self.notifications)

    def exit_when_empty(self):
        # print('exit check')
        if not any(RssNotifier.notifications):
            self.loop.quit()
            return False
        return True


CACHE_FILE = '.cache.json'

parser = ArgumentParser()
parser.add_argument(
    '-u', '--url',
    default="http://hackernews.demos.monkeylearn.com/feed.xml?",
    dest='url',
    type=str,
    help='The url to be parsed'
)
parser.add_argument(
    '-l', '--lines',
    default=10,
    dest='lines',
    type=int
)

parser.add_argument(
    '-w', '--width',
    default=80,
    dest='width',
    type=int,
    help='The horizontal limit'
)
parser.add_argument(
    '-p', '--prefix',
    default='- ',
    dest='prefix',
    type=str,
    help='A prefix attached each feed'

)

parser.add_argument(
    '-i', '--ignore',
    default='',
    dest='ignore',
    type=str,
    help='Useless string to remove'
)

parser.add_argument(
    '-n', '--disable-notifications',
    default=True,
    dest='notifications',
    action='store_false',
    help='Disable notifications (default True)'
)

parser.add_argument(
    '-r', '--rss-label',
    default='RSS',
    dest='rss_label',
    type=str,
    help='A simple label for what is fetching'

)


def get_label(entry):
    if entry.get('tags'):
        label = '{}: '.format(entry.get('tags').pop()['term'])
    else:
        label = ''
    return label


def long_title_clean(title):
    if len(title) > options.width:
        return (title[:options.width] + '\n' +
                ' ' * (len(options.prefix)) +
                long_title_clean(title[options.width:].strip()))
    return title


def translate_name(url):
    return '.' + ''.join([x for x in url if x in alphabet]) + '.cache'


def save_cache(new_cache, key):
    cache_file = get_cache_file()
    cache_file[key] = new_cache
    with open(CACHE_FILE, 'w') as f:
        json.dump(cache_file, f)


def get_cache(key):
    return get_cache_file()[key]


def get_cache_text(key):
    return '\n'.join((x for x, _ in get_cache(key)))


def get_cache_file():
    try:
        with open(CACHE_FILE, 'r') as f:
            return json.load(f)
    except:
        return {}


def notify(new_rss):
    notifier = RssNotifier(options.rss_label)
    for rss, url in new_rss:
        notifier.send_rss(rss, url)
    notifier.loop.run()


def ignore_pattern(title):
    return title.replace(options.ignore, '')


def parallel_notifications(new_rss):
    if any(new_rss) and options.notifications:
        if fork() == 0:
            notify(new_rss)


def parse_print_rss(feed):
    new_cache = []
    for entry in feed['entries']:
        if len(new_cache) >= options.lines:
            break
        label = get_label(entry)
        output = '{}{}{!s}'.format(options.prefix, label, entry.title)
        title = long_title_clean(ignore_pattern(output))
        if title not in new_cache:
            new_cache.append([title, entry['link']])
            print(title)

    return new_cache


if __name__ == '__main__':
    loop = GObject.MainLoop()
    options = parser.parse_args()
    feed = feedparser.parse(options.url)
    keyname = translate_name(options.url)
    if not any(feed['entries']):
        cache = get_cache_text(keyname)
        print(cache)
        sys.exit(0)

    new_cache = parse_print_rss(feed)
    old_cache = get_cache(keyname)
    new_rss = [x for x in new_cache if x not in old_cache]
    new_rss = new_cache  # force use the new_cache
    # the paralell part going here
    parallel_notifications(new_rss)
    save_cache(new_cache, keyname)

懒人专属代码

import os
if os.fork() == 0:
     os.setsid()
     while True:
         pass
print('shorter')

提示:在conky上,从未发生过“shorter”的情况,因为它会等待子进程!

尝试受挫

在第一次尝试中,我使用了multprocessing库来设置一个新进程作为守护进程(以便主程序不需要完成),但这没有起作用。顺便说一下,现在这创建了另一个问题:当主程序完成时,进程并行也会结束,现在我没有更多的通知了(或者只是因为进程已经完成,点击例程就不起作用了)!!! D:

编辑-1

如果我尝试在终端中运行,则使用fork可以正常工作!但是我在conky中运行真的有问题!为什么?此外,在sublime中,我只有相同的行为:只有子进程退出时,父进程才会退出


请使用这个库:https://pypi.python.org/pypi/python-daemon/ - Valentin Lorentz
你能给我一个关于这个用法的例子来解决我的问题吗?谢谢。 - Manoel Vilela
1个回答

0
简短回答:将线程变为守护进程。
我使用的片段在其他线程中运行,我只是按照Valentin Lorentz的建议这样做。
from daemon import DaemonContext
with DaemonContext():
      paralell_notifications(new_rss)

一直都很正常,但只有在终端和Sublime里。我的主要问题是conky,仍然存在同样的问题。似乎是conky上的一个bug。

为什么呢?因为他said

守护进程是一种后台非交互式程序。它与任何交互式用户的键盘和显示器分离。用于表示后台程序的“守护进程”一词源自Unix文化,它并不普遍存在。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接