多线程使其运行一次

发布于 2025-01-28 12:56:35 字数 2185 浏览 5 评论 0原文

我非常感谢每个为我的第一个问题做出贡献的人,尽管我不以某种方式询问您会忍受我。

我有一个数据库,其中一个表名称合同是合同和组中的数据库,即每个组都有一个合同。

我需要帮助,以便一旦启动机器人,它就会检查数据库和合同列表,然后同时为每个组和他们自己的合同运行任务... 下面的代码是我能够进行的,但它仅适用于一个组,

from web3 import Web3
import json,os,sys

import datetime

import requests
from db import *
import telebot


bot = telebot.TeleBot("Telegram bot api will be here")



 
bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))




class BuyBot():
    def init(self,token,group):
        self.token = token
        self.group = group
        
    
    def main(self):
        print(f"EVENTS RUNNING FOR {self.token}")
        event_filter = web3.eth.filter({"address": web3.toChecksumAddress((self.token).upper()), "block_identifier": 'pending', })
        
        
        self.log_loop(event_filter, 2)
               
        
           
    def log_loop(self,event_filter, poll_interval):
        try:
            while True:
                for event in event_filter.get_new_entries():
                    self.handle_event(event)
                    break
                time.sleep(poll_interval)
        except Exception as p:
            print(p)
            self.main()
            
    def handle_event(self,event):
        txn = json.loads(Web3.toJSON(event))
        hash = txn['transactionHash']
    
        if len(hash) > 0:
            transaction_recipt = web3.eth.wait_for_transaction_receipt(hash)
            trs = web3.toHex(transaction_recipt['transactionHash'])
            usl_hash = f"https://bscscan.com/tx/{trs}"
            bot.send_message(self.group,url_hash)


def main():
    while True:
        print("am here")
        con = bot_db()
        mc= con.cursor()
        mc.execute('SELECT * FROM Contracts')
        rows = mc.fetchall()
        for all in rows:
            group = all[1]
            ca = all[0]
            
            check_ = BuyBot(ca,group)
            check_.main()

bot.polling(none_stop=True)

if name == "main":
    try:
        print("Started")
        main()
    except Exception:
        print("rebooted")
        main()

请帮助我需要它来聆听每个小组在每个小组合同的同时聆听BSC链Web3上的事件。

如下所述,如果您不明白我的意思,请忍受我

I really appreciate everyone who contributed to my first question though am new here If I didn't ask in a way you can understand do bear with me.

I have a database and in it a table name contracts which the data in the rows are contract and group, i.e each each group has a single contract.

I need help so that once the bot is started it checks the database and list of contracts in it then it runs task for each group and their own contracts at same time...
This codes below is what I was able to work on but it just works for only one group

from web3 import Web3
import json,os,sys

import datetime

import requests
from db import *
import telebot


bot = telebot.TeleBot("Telegram bot api will be here")



 
bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))




class BuyBot():
    def init(self,token,group):
        self.token = token
        self.group = group
        
    
    def main(self):
        print(f"EVENTS RUNNING FOR {self.token}")
        event_filter = web3.eth.filter({"address": web3.toChecksumAddress((self.token).upper()), "block_identifier": 'pending', })
        
        
        self.log_loop(event_filter, 2)
               
        
           
    def log_loop(self,event_filter, poll_interval):
        try:
            while True:
                for event in event_filter.get_new_entries():
                    self.handle_event(event)
                    break
                time.sleep(poll_interval)
        except Exception as p:
            print(p)
            self.main()
            
    def handle_event(self,event):
        txn = json.loads(Web3.toJSON(event))
        hash = txn['transactionHash']
    
        if len(hash) > 0:
            transaction_recipt = web3.eth.wait_for_transaction_receipt(hash)
            trs = web3.toHex(transaction_recipt['transactionHash'])
            usl_hash = f"https://bscscan.com/tx/{trs}"
            bot.send_message(self.group,url_hash)


def main():
    while True:
        print("am here")
        con = bot_db()
        mc= con.cursor()
        mc.execute('SELECT * FROM Contracts')
        rows = mc.fetchall()
        for all in rows:
            group = all[1]
            ca = all[0]
            
            check_ = BuyBot(ca,group)
            check_.main()

bot.polling(none_stop=True)

if name == "main":
    try:
        print("Started")
        main()
    except Exception:
        print("rebooted")
        main()

Please do help I need it to listen to events on the Bsc chain web3 for each group at same time with each group contract.

As I stated below please do bear with me if you don't understand what I meant

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾听心声的旋律 2025-02-04 12:56:35

能够完成我需要的多处理完成我需要的工作。

这是文档

https: > https:> https:> https: //docs.python.org/3/library/multiprocessing.html

Was able to do what I needed multiprocessing did the job I needed ✅

Here is the documentation

https://docs.python.org/3/library/multiprocessing.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文