python - 当模块A中的线程完成处理时如何通知模块B?
问题描述
我正在构建一个简单的爬虫,其中模块A
每 24 小时扫描一次源以查找更改,并将新数据附加到磁盘上的 json 文件中。我使用模块B
通过处理数据来更改数据。B
我的问题是,当模块中的线程完成时,我需要以某种方式通知模块A
。作为一名 JS 开发人员,我自然会求助于在模块中实现监听器的事件,B
但不幸的是 python 没有。
最初我已经将模块转换为类,然后B
通过扩展A
,但很快意识到这并不好,因为某些模块:C
,D
- 也会订阅A
,将触发继承的同步逻辑(负责扫描并保存到本地 json)这是不需要的。
所以我想坚持使用模块和一种简单的方法来通知n
多个模块,只要在模块中完成同步A
。
## Module A (storyHandler.py)
def init(self):
self.startCronJob()
def startCronJob(self):
...
t = threading.Timer(secs, self.syncStories)
print('Cron for synching stories started ...')
t.start()
def syncStories(self):
...
json = stories.to_json(orient='records')
with open('data/stories/stories.json', 'w') as f:
f.write(json)
print('synching stories done!')
#notify(stories) # <-- need to trigger a notification here
## Module B (storyCategoryClassifier.py)
def onNotify(stories): # module A should trigger this
print('stories updated, start re-training category classifier')
trainCategoryClassifier(stories)
def trainCategoryClassifier(stories):
...
## Module C (storyTypologyClassifier.py)
def onNotify(stories):
print('stories updated, start re-training typology classifier')
trainTypologyClassifier(stories)
def trainTypologyClassifier(stories):
...
非常感谢您的帮助!
解决方案
推荐阅读
- python - 使用 Bokeh 在 Python 中对数据进行分组并将其可视化
- javascript - 收益 TakeEvery 与收益全部
- vue.js - Uncaught (in promise) TypeError: Cannot read property '$store' of undefined
- python - 编写一个名为 justify 的函数,它接受一个字符串列表和一个文件大小,然后返回一个字符串
- flutter - Dart:如何识别对象?
- r - 我应该如何在 R 中为逻辑回归选择特征?
- java - 如何在递归 Java 中使用用户输入
- git - 我想在本地撤消与 master 的合并,但它给了我一个致命错误
- python - Sympy.solve 返回:“[]”
- types - 从 rescript 中的变体类型访问密钥