[英]Run multiple terminals from python script and execute commands (Ubuntu)
What I have is a text file containing all items that need to be deleted from an online app.我拥有的是一个文本文件,其中包含需要从在线应用程序中删除的所有项目。 Every item that needs to be deleted has to be sent 1 at a time.
每个需要删除的项目都必须一次发送 1 个。 To make deletion process faster, I divide the items in text file in multiple text files and run the script in multiple terminals (~130 for deletion time to be under 30 minutes for ~7000 items).
为了加快删除过程,我将文本文件中的项目划分为多个文本文件,并在多个终端中运行脚本(删除时间约为 130,对于 ~7000 项,删除时间不到 30 分钟)。
This is the code of the deletion script:这是删除脚本的代码:
from fileinput import filename
from WitApiClient import WitApiClient
import os
dirname = os.path.dirname(__file__)
parent_dirname = os.path.dirname(dirname)
token = input("Enter the token")
file_name = os.path.join(parent_dirname, 'data/deletion_pair.txt')
with open(file_name, encoding="utf-8") as file:
templates = [line.strip() for line in file.readlines()]
for template in templates:
entity, keyword = template.split(", ")
print(entity, keyword)
resp = WitApiClient(token).delete_keyword(entity, keyword)
print(resp)
So, I divide the items in deletion_pair.txt and run this script multiple times in new terminals (~130 terminals).因此,我将 deletion_pair.txt 中的项目分开,并在新终端(~130 个终端)中多次运行此脚本。 Is there a way to automate this process or do in more efficient manner?
有没有办法使这个过程自动化或以更有效的方式进行?
I used threading to run multiple functions simultaneously:我使用线程同时运行多个函数:
from fileinput import filename
from WitApiClient import WitApiClient
import os
from threading import Thread
dirname = os.path.dirname(__file__)
parent_dirname = os.path.dirname(dirname)
token = input("Enter the token")
file_name = os.path.join(parent_dirname, 'data/deletion_pair.txt')
with open(file_name, encoding="utf-8") as file:
templates = [line.strip() for line in file.readlines()]
batch_size = 20
chunks = [templates[i: i + batch_size] for i in range(0, len(templates), batch_size)]
def delete_function(templates, token):
for template in templates:
entity, keyword = template.split(", ")
print(entity, keyword)
resp = WitApiClient(token).delete_keyword(entity, keyword)
print(resp)
for chunk in chunks:
thread = Thread(target=delete_function, args=(chunk, token))
thread.start()
It worked, Any one has any other solution.它奏效了,任何人都有任何其他解决方案。 please post or if the same code can be written more efficiently then please do tell.
请发帖,或者如果可以更有效地编写相同的代码,请告知。 Thanks.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.