[英]How can I send a signal from one Python script to trigger another?
I am currently running a python script that interacts with an API to fetch inventory prices and when certain conditions are met, I send a post request to place this item based on its unique identification number into a cart.我目前正在运行一个 python 脚本,该脚本与 API 交互以获取库存价格,当满足特定条件时,我发送一个发布请求以根据其唯一标识号将此项目放入购物车。
The issue I am having is that after the item is in the cart, I am no longer able to interact with the API to post a buy, instead I have to go through the checkout process.我遇到的问题是,在商品放入购物车后,我无法再与 API 互动以发布购买信息,而我必须通过结帐流程与 go 互动。 This can easily be done with Selenium, but the inherent issue here is speed because I must launch a chrome window, navigate to the site, login, etc.
这可以通过 Selenium 轻松完成,但这里的固有问题是速度,因为我必须启动 chrome window,导航到站点,登录等。
Instead, what I would like to do is have two separate python scripts with one running the fetch and adding items to a checkout carts and the other running selenium, already logged into a buying account.相反,我想做的是有两个单独的 python 脚本,一个运行获取并将项目添加到结帐购物车,另一个运行 selenium,已经登录到购买帐户。 Once an item is placed in the cart, the script will send a signal to the other (already running, awaiting a command) selenium bot.
将商品放入购物车后,脚本将向另一个(已在运行,正在等待命令)selenium 机器人发送信号。
This may be possible with node, but have not done anything like this before - if someone could point me in the right direction it would be appreciated!这对于节点来说是可能的,但之前没有做过这样的事情——如果有人能指出我正确的方向,我将不胜感激!
I guess u can use the multiprocessing
module in Python to run the two scripts in separate processes, and then use the Queue
class from the multiprocessing
module to communicate between the processes.我想你可以使用 Python 中的
multiprocessing
模块在单独的进程中运行这两个脚本,然后使用multiprocessing
模块中的Queue
class 在进程之间进行通信。
For instance, here buy
is being invoked after receiving a signal from the first script:例如,这里的
buy
在从第一个脚本接收到信号后被调用:
Script to add items to the cart:将商品添加到购物车的脚本:
from multiprocessing import Process, Queue
def main():
# Fetch inventory prices and add items to the cart
# When an item is added to the cart, put a message on the queue
queue.put("ITEM_ADDED")
if __name__ == '__main__':
# Create a queue to communicate with the other process
queue = Queue()
# Start the other process
p = Process(target=buy, args=(queue,))
p.start()
# Run the main script
main()
Another script to initiate the buy process.启动购买过程的另一个脚本。
def buy(queue):
# Wait for messages from the other process
while True:
message = queue.get()
if message == "ITEM_ADDED":
# Use selenium to buy the item from the cart
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.