Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

"The way of the world is to praise dead saints and prosecute live ones." -- Nathaniel Howe


devel / comp.lang.python / Re: Using a background thread with asyncio/futures with flask

SubjectAuthor
o Re: Using a background thread with asyncio/futures with flaskFrank Millman

1
Re: Using a background thread with asyncio/futures with flask

<mailman.10.1711106668.3468.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=29718&group=comp.lang.python#29718

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: frank@chagford.com (Frank Millman)
Newsgroups: comp.lang.python
Subject: Re: Using a background thread with asyncio/futures with flask
Date: Fri, 22 Mar 2024 13:23:43 +0200
Lines: 63
Message-ID: <mailman.10.1711106668.3468.python-list@python.org>
References: <8a7231df-c30c-4fa6-9daa-546b3453b547@thomasnyberg.com>
<7af73918-8209-4393-8aa2-140940567456@chagford.com>
<e7140924-fcfd-4816-b905-8e950ab2dfdd@chagford.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de QZf8JsVG1RTMeZVqiG6b1QpUd9+mlpa9eKgS/zWhwzTg==
Cancel-Lock: sha1:1igxaoNjNqYs3a/PfeX7w7/VMsk= sha256:/omubm73wYfUCIbkP1wsYztxiPmx9zuFK55QfEw4Y9Y=
Return-Path: <frank@chagford.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=chagford.com header.i=@chagford.com header.b=mjfIeXTk;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'def': 0.04; 'loop': 0.07;
'blocking': 0.09; 'instances': 0.09; 'threads': 0.09; 'import':
0.15; 'app,': 0.16; 'expert.': 0.16; 'from:addr:chagford.com':
0.16; 'from:addr:frank': 0.16; 'from:name:frank millman': 0.16;
'functions.': 0.16; 'message-id:@chagford.com': 0.16; 'millman':
0.16; 'received:196.35': 0.16; 'received:196.35.198': 0.16;
'received:197.90': 0.16; 'received:197.90.32': 0.16;
'received:197.90.32.26': 0.16; 'received:synaq.com': 0.16;
'subject:thread': 0.16; 'tasks.': 0.16; 'wrote:': 0.16; 'problem':
0.16; 'pm,': 0.19; 'to:addr:python-list': 0.20; 'version': 0.23;
'task': 0.26; 'bit': 0.27; 'background': 0.28; 'header:User-
Agent:1': 0.30; 'think': 0.32; 'python-list': 0.32;
'subject:Using': 0.32; 'but': 0.32; 'work.': 0.34; 'header:In-
Reply-To:1': 0.34; 'way': 0.38; 'put': 0.38; 'use': 0.39; 'case.':
0.40; 'something': 0.40; 'skip:o 10': 0.61; 'skip:i 20': 0.62;
'here': 0.62; 'skip:o 20': 0.63; 'your': 0.64; 'similar': 0.65;
'await': 0.69; 'received:196': 0.69; 'skip:a 40': 0.70; 'skip:f
30': 0.71; 'future': 0.72; 'true:': 0.84
ARC-Seal: i=1; cv=none; a=rsa-sha256; d=synaq.com; s=securemail; t=1711106666;
b=X7u6639PULptfVnkHYqmtB1E3z4qliIT2FXdeHVxWn6PsG0tjp59skctYXkzo/WrcCsFTL5LuO
xMTL0upM/eeQZM3ytPrluM/i3PJIcPjhsnFKpSb+p/nxntUQcp+OZTlZPXfyIf6Z43BDzX0s+Y
FXmoKRuqmw28wpx1QMiUG2E099RtCCpkr2+Fn9rvcb/OucNdE1q4UHnXQk0A+H5L8IapCpZ9sr
6+HG5sZP8F65SlcMS3GeG7llVD5NuyZuYuMFEEhgDlAtvvwyeRvvtk/MIcDq8dxt32XyKkLK4j
LXnftcyEYUUHOxRdCUzrU1itizcG4wWz0rvTKmvQfX6LmA==;
ARC-Authentication-Results: i=1; synaq.com;
iprev=fail smtp.remote-ip=197.90.32.26;
auth=pass (PLAIN) smtp.auth=frank@chagford.com;
arc=none
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=synaq.com; s=securemail;
t=1711106666;
bh=M9xs3CqJrL7FSa0cfrhoKOdKnY1XhmuQMUtJOgNJTPc=;
h=Content-Transfer-Encoding:Content-Type:In-Reply-To:From:References:To:
Subject:MIME-Version:Date:Message-ID:DKIM-Signature;
b=qXI3aQR04Pr6+JmckTVl7yQGj1CDpt1lJQ21J3eAmnKkc+nkOw6umwXMix0g8Du3vfqpv6ewZe
RJhMXCqqvoejA35jIlGlT2Do4qbLqBG1QPlDLPyl/6Ke86omYyPMIFqqFLjLGVo0scZgP4SSQj
t2w4BPsfRbMdigBUHP26mzUL9POu5XIYCgovq0x49LM5Z+S/YU59rbDvluQ15Y4WqzidTEG/HC
8L8TESNSxw80N8ywvD5OUR+WU56LufZv0QZ44SwcVyfAlMGDHq+v8Vv+Dhx+mbPqqTZMY9xkvm
Cn4mmSB/aTK68quG+c2Q3xGU59PGVaw/vwtPz29Z3EEtuA==;
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed;
d=chagford.com; s=securemail; h=In-Reply-To:From:To:Date:Message-ID;
bh=hZWdQpVNae0sccn8OhP3Y1tdDU3n+02K3blmixiBi/c=; b=mjfIeXTkCQUJjdfkUvrqh7RF6M
seAvudXg7mTR8/cCxzwDXCNMDot1SbAGL1yxovVvIR5M9XCnSVlFyP5Qnhxz8MozTJS8hIDURe0Gi
sxqN4h63gyfuEyH5nyXmfGzmfKcLf+Wfpacrbehqo/KqJNqYWdwpMfrlN/pBFCGuydJlBBJvS7Gxz
q2ZuNZRldscMuLtDvw/XAiRMdK4UIfSVr4AizXky1NsHuZICAeNrtkWyWx8zGu6gJrfRCOBSqT2rw
13qfVSVoufGd8faHcrvnBN5xblXosdL5Qh2s+PW7DcAMb9SPhXtdlQWyMEJaQwU02bUMywfYFnmdG
lIwm5DvA==;
Authentication-Results: synaq.com; iprev=fail smtp.remote-ip=197.90.32.26;
auth=pass (PLAIN) smtp.auth=frank@chagford.com;
arc=none
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <7af73918-8209-4393-8aa2-140940567456@chagford.com>
X-Red-Router: yes
X-SYNAQ-Pinpoint-Information: Please contact SYNAQ for more information
X-SYNAQ-Pinpoint-ID: 1rnd08-0007C1-2I
X-SYNAQ-Pinpoint: No virus infections found
X-Pinpoint-From: frank@chagford.com
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <e7140924-fcfd-4816-b905-8e950ab2dfdd@chagford.com>
X-Mailman-Original-References: <8a7231df-c30c-4fa6-9daa-546b3453b547@thomasnyberg.com>
<7af73918-8209-4393-8aa2-140940567456@chagford.com>
 by: Frank Millman - Fri, 22 Mar 2024 11:23 UTC

On 2024-03-22 12:09 PM, Frank Millman via Python-list wrote:
>
> I am no expert. However, I do have something similar in my app, and it
> works.
>
> I do not use 'await future', I use 'asyncio.wait_for(future)'.
>

I tested it and it did not work.

I am not sure, but I think the problem is that you have a mixture of
blocking and non-blocking functions.

Here is a version that works. However, it is a bit different, so I don't
know if it fits your use case.

I have replaced the threads with background asyncio tasks.

I have replaced instances of queue.Queue with asyncio.Queue.

Frank

===============================================

import asyncio

in_queue = asyncio.Queue()
out_queue = asyncio.Queue()

async def worker():
print("worker started running")
while True:
future = await in_queue.get()
print(f"worker got future: {future}")
await asyncio.sleep(5)
print("worker sleeped")
await out_queue.put(future)

async def finalizer():
print("finalizer started running")
while True:
future = await out_queue.get()
print(f"finalizer got future: {future}")
future.set_result("completed")
print("finalizer set result")

async def main():
asyncio.create_task(worker()) # start a background task
asyncio.create_task(finalizer()) # ditto
future = asyncio.get_event_loop().create_future()
await in_queue.put(future)
print(f"main put future: {future}")
result = await asyncio.wait_for(future, timeout=None)
print(result)

if __name__ == "__main__":
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())

# this is the preferred way to start an asyncio app
asyncio.run(main())

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor