繁体   English   中英

带有计划的 Python 调度作业以及套接字模块不起作用

[英]Python Scheduling jobs with schedule along with socket module not working

我正在尝试创建一个套接字服务器 (TCP/IP) 并在其中基于来自客户端的少量数据我正在安排一些后台作业。

以下代码正在运行->

import schedule
import time

def test1():
    print('hi from 1')
 
def test2():
    print('hi from test2')

while True:
    schedule.run_pending()
    time.sleep(1)

然后我尝试使用套接字服务器执行以下操作,然后它不执行作业/功能。 有人可以帮助我这里发生了什么。

不工作的代码

import schedule
import time
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('localhost', 8009)
print('starting up on {} port {}'.format(*server_address))
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

def test1():
    print('hi from 1')
 
def test2():
    print('hi from test2')
 
schedule.every(1).minutes.do(test1)
schedule.every(2).minutes.do(test2)
 
while True:
    schedule.run_pending()
    time.sleep(1)
 
    print('waiting for a connection')
    connection, client_address = sock.accept()
    
    data = connection.recv(1024)
    result =  data.decode('utf-8')
    print('data recived from clinet : ', result)

我想要实现的是我想创建 python 套接字服务器,它将接受来自节点客户端的请求,并基于客户端数据我想在 python 中安排一些工作。 为此,我使用套接字,从 python 调度模块来创建套接字服务器并分别调度作业和节点 js 客户端的网络模块,以便将数据发送到 python 服务器。

请更详细地解释您的问题。 sock.accept 阻塞,所以循环阻塞,这是你的问题吗?

为了防止程序阻塞,您可以在单独的线程中运行调度程序循环,也可以在单独的线程中运行接受循环。 创建一个主线程来管理您的子线程。 看看模块线程

也许使用其他可以处理线程的调度程序库是有意义的,请参见此处

披露:我是调度程序库的作者之一

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM