Python中的Socket并发

2024-05-23 14:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用这个库:ESCPOS Printer for Python使用Ethernet类与printer进行通信:CLASS ESCPOS ETHERNET

我尝试使用简单线程执行许多调用,如上图所示:

from time import sleep
import requests
import threading
import datetime
from escposprinter import *
from escposprinter.escpos import EscposIO, Escpos


NumeroPartenza = 0
NumeroFine = 9
NumeroStampePerStampante = 1  


printerPort = 9100
dictionaryStampanti = {'printer1' : '192.168.0.196','printer2' : '192.168.0.197',  'printer3' : '192.168.0.198'}


def printSimple(threadName, stampanteIp):
    with EscposIO(printer.Network(stampanteIp, printerPort)) as p:
        for iteration in range(NumeroStampePerStampante):
            p.set(font='a', codepage='cp1251', size='normal', align='center', bold=True)
            p.writelines('')
            p.writelines('')
            p.writelines('')
            p.writelines("{0}: {1}".format( threadName, datetime.datetime.now().strftime("%H:%M:%S.%f")))
            p.writelines('')
            p.writelines('')
            p.writelines('')



#Testa stampante semplice
for Ordine in range(NumeroPartenza, NumeroFine):
    stringToPrint = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
    threading.Thread(target=printSimple, args=("Thread1: " + stringToPrint, dictionaryStampanti.get('printer1')), ).start()

    threading.Thread(target=printSimple, args=("Thread2: " + stringToPrint, dictionaryStampanti.get('printer1')), ).start()

    threading.Thread(target=printSimple, args=("Thread3: " + stringToPrint, dictionaryStampanti.get('printer1')), ).start()

但它经常失败,给我一个错误:Err 60连接超时,我的意思是,为什么使用线程并发调用打印机我遇到这个问题?你知道吗

有人能帮我解决这个问题吗?你知道吗

谢谢


Tags: fromimporttargetforwritelinesgetdatetimeargs