我使用Python实现了异步拉取订阅者。这是基本代码
<script>
// it´s giving the correct hour
var getserver = new Date();
getserver.setHours(getserver.getHours());
var newtets = getserver.toUTCString();
alert(newtets);
// using a basic countdown
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
/*
var now = new Date();
now.setHours(now.getHours());
var newnow = now.toUTCString();
i tried to replace now with newnow but is not working, how can i transform this
*/
// Find the distance between now an the count down date
var distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Output the result in an element with id="demo"
document.getElementById("demo").innerHTML = days + "d " + hours + "h "
+ minutes + "m " + seconds + "s ";
// If the count down is over, write some text
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
</script>
我需要像
一样打印A,
消息
乙
A
消息
乙
(我需要按顺序运行)或通过给定的没有线程接收消息。我找不到限制线程的方法。由于许多线程,我的程序给出分段错误。
我如何控制没有线程接收消息。
答案 0 :(得分:1)
如果需要处理回调按顺序运行,那么使用消息传递模型比修改订户内部更好。如果将接收到的消息推送到显式queue.Queue,则可以确保只有一个工作线程从该队列中退出,并且一次仅处理一个。但是请注意,尽管只有一份订购工作为您提供了“一次一次”的处理保证,但并不能为您提供任何订购保证。相对于邮件的发布顺序,仍然可以以任意顺序处理邮件。
答案 1 :(得分:0)
使用政策可以解决问题
from google.cloud import pubsub_v1
from concurrent import futures
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project, subscription_name)
def callback(message):
print (str(message.data) + " " + str(threading.current_thread()))
message.ack()
flow_control = pubsub_v1.types.FlowControl(max_messages=10)
executor = futures.ThreadPoolExecutor(max_workers=5)
policy = pubsub_v1.subscriber.policy.thread.Policy(subscriber, subscription_path, executor=executor, flow_control=flow_control)
policy.open(callback)
我们可以使用 max_workers 设置最大线程数。还可以设置流量控制设置。