gpt4 book ai didi

python - 我如何并行运行一个函数并且在主程序退出后他仍然继续运行?

转载 作者:行者123 更新时间:2023-11-28 19:13:14 25 4
gpt4 key购买 nike

长话短说

如何使用多处理库或其他方式使并行进程独立于主进程运行? (示例代码现在使用的是fork)

悠久的历史

好吧,我正在为我的 conky 配置编写一个小脚本,其中一个是 rss_parser.py,它获取一组标题/url 并放在 stdout 上。最近,我想知道当 RSS 列表中出现新内容时,使用 GTKNotify 激活新通知。

但是我这里有一些问题。我设置了一个打开链接的 Action ,所以当我的通知出现时,我可以点击并打开浏览器。这部分代码需要等待用户点击或通知接近完成,为此在适当的事件循环中运行。但是,这与其他事情有冲突:要更新我的 conky 我需要我的主脚本在不期望通知部分的情况下完成!所以我的想法是将通知并行放置。

来自 hell 的密码

我用这个完整的代码试过了 code : (检查 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')

ps.: 在 conky 上,永远不会发送“更短”,因为这永远不会发生,他等待 child ! (或类似的东西)

遭遇尝试

在第一次尝试时,我使用multprocessing 库将一个新进程设置为守护进程(对主程序而言不期望完成),但这不起作用。顺便说一句,这现在造成了另一个问题:当主程序完成时,进程并行完成,现在我没有更多通知(或者简单的点击例程不起作用,因为进程已经完成)! ! D:

编辑-1

如果我尝试在终端中运行,使用 fork 效果很好!但是我在 conky 中运行时遇到了一个真正的问题!为什么这个?在sublime中,除此之外,我有同样的行为:父进程仅在子进程退出时退出

最佳答案

简短回答:守护线程

我以前在其他线程中运行的片段,我只是把这个(正如 Valentin Lorentz 所建议的)

from daemon import DaemonContext
with DaemonContext():
paralell_notifications(new_rss)

并且总是工作正常,但是,仅在终端和 sublime 中。我的主要问题是 conky,我仍然有同样的问题。似乎是 conky 上的错误。

这是为什么?因为他 said :

a daemon is a background, non-interactive program. It is detached from the keyboard and display of any interactive user. The word daemon for denoting a background program is from the Unix culture; it is not universal.

关于python - 我如何并行运行一个函数并且在主程序退出后他仍然继续运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37308905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com