Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading issue - socket is already closed #206

Closed
mmvds opened this issue Mar 31, 2022 · 2 comments
Closed

Threading issue - socket is already closed #206

mmvds opened this issue Mar 31, 2022 · 2 comments

Comments

@mmvds
Copy link

mmvds commented Mar 31, 2022

I've tried to use Threading to increase an execution speed
simple example - get hashes for blocks in range(11704730, 11704741)
I wrote the following:

import threading
import concurrent.futures
from substrateinterface import SubstrateInterface

blocks = list(range(11704730, 11704741))

kusama_substrate = SubstrateInterface(url= 'wss://kusama-rpc.polkadot.io/', ss58_format=2)

def hash_block(current_block):
    try:
        block_info = kusama_substrate.get_block(block_number=current_block)
        if block_info:     
            print(current_block, block_info['header']['hash'])
        else:
            print(current_block, "no hash")
    except Exception as e:
        print(e)

with concurrent.futures.ThreadPoolExecutor(4) as executor:
    executor.map(hash_block, blocks)

when I run, it works well for several blocks and after that I get "socket is already closed" error

11704733 0x00721eb05b33a6684afbbc0aae101be3e8cb1e5648a7dae780a998fca9633ea2
11704731 0xcbbb4561a26e9b3f74231b1efd26af3b1ee5155f05c6d2674d2245e28994ac44
11704732 0x132e43ca3a154e102de53877784237f3528f3bee1082a7e20aeccd330dbf77c5
11704730 0x001de1a436000e1b537141ac8b05390bfcd8df76cbf899910f03d95ebefe9814
11704734 0xf0939ca7baf0483d338a1a139c5d89905fcdf47fecac502710d0bea52306375e
11704735 0x1fd6b1a03cc066c353df3379db32469f5ba75d1cdbeaf704eea0b2d9128c0b4e
11704736 0x630ab2618541c7cc3f7bc55b0e691d1514de0d654670c30c262b2cade0ae67ed
11704737 0xeeb47a564f9f0c7ae09a411ab751ba45051a4646b110ef97efeeafebc1d1a6f0
Connection to remote host was lost.
socket is already closed.
socket is already closed.

so, we lost several blocks
is there any way how to avoid this issue?

@arjanz
Copy link
Member

arjanz commented Apr 1, 2022

I tested your script with a local node endpoint and experienced no connection problems, so I guess it is a (probably intended) throttling limitation of the public endpoints..

I guess the best solution is to host your own nodes, as you will probably improve performance significantly even without using threads.

@mmvds
Copy link
Author

mmvds commented Apr 1, 2022

ok, thank you very much! will check it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants