'Multi thread making it run all once

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



Solution 1:[1]

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

Solution 2:[2]

I think you are over complicating it. If i understand correctly you have BaseTest class because of common test cases between A and B. @Test, and almost all other junit annotations, is inherited from super classes, unless the method is overridden. In this case all you need is creating concrete implementations of BaseTest. JUnit will initialize test class instances, run all @Test methods, etc.

public class TestA extends BaseTest<Integer> {
  //init stuff if needed
}

And other class

public class TestB extends BaseTest<String> {
  //init stuff if needed
}

And that's it, the junit runner will run all @Test methods from BaseTest for TestA and then for TestB.

Solution 3:[3]

If your objective is to run all @Test methods that are in an abstract class, and all extending classes, then that is already what happens. You can test this through any IDE (Intellij/Eclipse)

If however the objective is to programmatically select N classes, and run their methods from Java directly, then you can do so as described below:

In my opinion, it is best to follow the other answer, as this allows report plugins to visually show you what has passed/failed, and statistics.

It depends on what you are trying to achieve:


 public class RunJUnit5TestsFromJava {

      SummaryGeneratingListener listener = new SummaryGeneratingListener();
  
      public void runASingleClass() {

          LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
              .selectors(selectClass(FirstUnitTest.class))
              .build();
            Launcher launcher = LauncherFactory.create();
            TestPlan testPlan = launcher.discover(request);
            launcher.registerTestExecutionListeners(listener);
            launcher.execute(request);
        }

     public void runAllClassesUnderPackage() {

            LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
              .selectors(selectPackage("com.package.where.your.test.classes.are"))
              .filters(includeClassNamePatterns(".*Test"))
              .build();
            Launcher launcher = LauncherFactory.create();
            TestPlan testPlan = launcher.discover(request);
            launcher.registerTestExecutionListeners(listener);
            launcher.execute(request);
        }
    }

 
    RunJUnit5TestsFromJava runner = new RunJUnit5TestsFromJava();
    runner.runAll();
    
    TestExecutionSummary summary = runner.listener.getSummary();
    summary.printTo(new PrintWriter(System.out));
    

Reference: https://www.baeldung.com/junit-tests-run-programmatically-from-java

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Chaosfire
Solution 3