K
K
kiddle2017-10-25 00:46:27
Python
kiddle, 2017-10-25 00:46:27

Under what circumstances does a stream close in Python?

Hello, I started getting acquainted with multithreading.
My program launches 3 threads, each one parses information. Why is this happening? If needed, I'll attach the code. And how many maximum threads can be run and how effective will it be?

for _ in range(thread_col):
                        t = Thread(target=parse_info_from_link)
                        t.start()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Dugin, 2017-10-25
@adugin

For parsing information, use asynchronous programming (in Python 3.5+, the standard asyncio module and the aiohttp external library) and don't mess around with multithreading nonsense. Here is a piece of code from my own websocket project:

async def run(self):
    if not self.chains:
        return
    command = f'{{"command": "subscribe", "channel": "{self.name}"}}'
    while True:
        await asyncio.sleep(len(self.exchange.markets)*random())
        try:
            async with websockets.connect(self.wss, max_queue=0) as self.websocket:
                await asyncio.wait_for(self.websocket.send(command), timeout=10)
                while True:
                    data = await asyncio.wait_for(self.websocket.recv(), timeout=20)
                    data = json.loads(data, cls=DeepDecoder)
                    asyncio.ensure_future(self.process_websocket_message(data))
        except asyncio.TimeoutError as error:
            print(f'{self.name}: WEBSOCKET_TIMEOUT: {error}')
        except websockets.ConnectionClosed as error:
            print(f'{self.name}: WEBSOCKET_CLOSED: {error}')
        except (asyncio.CancelledError, KeyboardInterrupt, SystemExit) as error:
            print(f'{self.name}: TASK_CANCELLED: {error}')
            self.clear()
            return
        except Exception as error:
            print(f'{self.name}: {error}')
        finally:
            self.clear()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question