简体   繁体   English

在其他线程之前启动一个线程,并使其在整个程序中始终运行

[英]Starting a thread before other threads and keep it running throughout the entire program

Apologies if i've worded the question a bit wrong, but I'm working on a script which automates the checkout process of a certain e-commerce website. 抱歉,如果我说的问题有点错,但是我正在开发一个脚本,该脚本可以自动执行某个电子商务网站的结帐过程。 However, I would need to run a seperate thread to harvest google recaptchas and store them in a queue since they will need to be used when sending POST requests. 但是,我将需要运行一个单独的线程来收集Google Recaptchas并将其存储在队列中,因为在发送POST请求时将需要使用它们。 Would it be possible to start only the harvesting thread when the program starts and then start the checkout process based on input (eg when the user hits enter or something)? 在程序启动时是否只能启动收割线程,然后根据输入启动结帐过程(例如,当用户按下Enter键时)?

Also since google recaptcha tokens expire after a certain amount of time, is there a way that I can pop it off the queue without having to keep polling the queue? 另外,由于Google Recaptcha令牌在一定时间后过期,是否有一种方法可以将其弹出队列而不必继续轮询队列?

Thanks in advance. 提前致谢。

Would it be possible to start only the harvesting thread when the program starts and... 程序启动时是否可以仅启动收获线程并...

A thread starts when your code starts it. 当您的代码启动线程时,线程就会启动。 If you write code that starts a new thread when your program starts, then it will start a new thread when your program starts. 如果您编写的代码在程序启动时启动了一个新线程,那么它将在程序启动时启动一个新线程。

If you need to insure that other threads do not start until the first thread has completed some task, then that's a different story. 如果您需要确保在第一个线程完成某些任务之前其他线程不会启动,那么情况就不一样了。 You will need to use some synchronization object such as a countdown latch to make the main thread (or other threads) wait for the task to be completed. 您将需要使用一些同步对象(例如倒数锁存器)使主线程(或其他线程)等待任务完成。

Since google recaptcha tokens expire after a certain amount of time, is there a way that I can pop it off the queue without having to keep polling the queue? 由于google recaptcha令牌会在一定时间后过期,有没有一种方法可以让我将其弹出队列而不必继续轮询队列?

I'd use a separate data structure for that: Probably a min heap to keep track of which object will be the next one to expire. 为此,我将使用一个单独的数据结构:可能是一个最小堆,用于跟踪哪个对象将是下一个到期的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM