Skip to content

Commit 5735a63

Browse files
authored
Prepare timeflies for the new asyncio mainloop (#95)
1 parent 422c785 commit 5735a63

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

.config/dotnet-tools.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"fable": {
12-
"version": "4.2.1",
12+
"version": "4.2.2",
1313
"commands": [
1414
"fable"
1515
]

examples/timeflies/Program.fs

+6-17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open FSharp.Control
66
open Fable.Python
77
open Fable.Python.TkInter
88
open Fable.Python.Queue
9+
open Fable.Python.AsyncIO
910

1011
type Msg =
1112
| Place of label: Label * x: int * y: int
@@ -30,7 +31,6 @@ let workerAsync (mb: MailboxProcessor<Event>) =
3031
messageLoop ()
3132

3233
let agent = MailboxProcessor<TkInter.Event>.Start (workerAsync)
33-
3434
let frame = Frame(root, width = 800, height = 600, bg = "white")
3535
frame.bind ("<Motion>", agent.Post) |> ignore
3636
frame.pack ()
@@ -47,7 +47,7 @@ let stream =
4747
let sink (ev: Notification<Label * int * int>) =
4848
async {
4949
match ev with
50-
| OnNext (label, x, y) -> queue.put (Place(label, x, y))
50+
| OnNext (label, x, y) -> label.place (x, y)
5151
| OnError (err) -> printfn $"Stream Error: {err}"
5252
| _ -> printfn "Stream Completed!"
5353
}
@@ -56,22 +56,11 @@ let mainAsync =
5656
async {
5757
use! disposable = stream.SubscribeAsync(sink)
5858

59-
let rec update () =
60-
let size = queue.qsize ()
61-
62-
for _ in 1..size do
63-
let msg = queue.get (false)
64-
65-
match msg with
66-
| Place (label, x, y) -> label.place (x, y)
67-
| _ -> ()
68-
69-
match size with
70-
| n when n > 0 -> root.after (1, update)
71-
| _ -> root.after (10, update)
59+
while true do
60+
while root.dooneevent(int Flags.DONT_WAIT) do
61+
()
7262

73-
root.after (1, update)
74-
root.mainloop ()
63+
do! Async.AwaitTask(asyncio.create_task(asyncio.sleep(0.005)))
7564
}
7665

7766
[<EntryPoint>]

examples/timeflies/TimeFlies.fsproj

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net7.0</TargetFramework>
66
<WarnOn>3390;$(WarnOn)</WarnOn>
77
</PropertyGroup>
88
<ItemGroup>
99
<Compile Include="Program.fs" />
1010
</ItemGroup>
1111
<ItemGroup>
12-
<PackageReference Include="Fable.Python" Version="4.0.0-theta-*" />
13-
<PackageReference Include="Fable.Core" Version="4.0.0-theta-*" />
12+
<PackageReference Include="Fable.Python" Version="4.*" />
13+
<PackageReference Include="Fable.Core" Version="4.*" />
1414
<PackageReference Include="FSharp.Control.AsyncRx" Version="1.6.5" />
1515
</ItemGroup>
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\src\Fable.Python.fsproj" />
18+
</ItemGroup>
1619
</Project>

examples/timeflies/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ authors = ["Dag Brattli <[email protected]>"]
77
license = "MIT"
88

99
[tool.poetry.dependencies]
10-
python = "^3.9"
10+
python = "^3.11"
1111
fable-library = ">=0.8.0"
1212
#fable-python = "*"
1313
#fsharp-control-async-rx = "*"

src/stdlib/TkInter.fs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module Fable.Python.TkInter
22

33
open Fable.Core
44

5+
type Flags =
6+
| DONT_WAIT = 2
7+
58
[<Import("Event", "tkinter")>]
69
type Event =
710
abstract member x: int
@@ -33,6 +36,7 @@ type Tk(screenName: string option) =
3336
member _.update() = nativeOnly
3437
member _.mainloop() = nativeOnly
3538
member _.after(msecs: int, callback: unit -> unit) = nativeOnly
39+
member _.dooneevent(flags: int) = nativeOnly
3640

3741
[<Import("Frame", "tkinter")>]
3842
type Frame(master: Misc) =

src/stdlib/asyncio/Tasks.fs

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ open Fable.Core
55
type Task<'T> =
66
inherit Future<'T>
77

8+
type Task =
9+
inherit Future<unit>
10+
811
type Coroutine<'T> =
912
inherit Awaitable<'T>
1013

@@ -15,7 +18,12 @@ type Coroutine<'T> =
1518

1619
type IExports =
1720
abstract create_task<'T> : fn: Coroutine<'T> -> Task<'T>
18-
abstract sleep: seconds: float -> result: 'T -> Future<'T>
21+
/// Translates a Python Task to a System.Threading.Tasks.Task
22+
abstract create_task: task: Task -> System.Threading.Tasks.Task
23+
/// Translates a Python Task<'T> to a System.Threading.Tasks.Task<'T>
24+
abstract create_task: task: Task<'T> -> System.Threading.Tasks.Task<'T>
25+
abstract sleep: seconds: float -> Task
26+
abstract sleep: seconds: float * result: 'T -> Task<'T>
1927
abstract run<'T> : main: Awaitable<'T> -> 'T
2028
abstract run<'T> : main: System.Threading.Tasks.Task<'T> -> 'T
2129

test/TestAsyncIO.fs

+16
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,19 @@ let ``test builder run int works`` () =
1919
let result = asyncio.run(tsk)
2020
result |> equal 42
2121

22+
[<Fact>]
23+
let ``test sleep works`` () =
24+
let tsk = task {
25+
do! asyncio.create_task(asyncio.sleep(0.1))
26+
return 42
27+
}
28+
let result = asyncio.run(tsk)
29+
result |> equal 42
30+
31+
[<Fact>]
32+
let ``test sleep with value works`` () =
33+
let tsk = task {
34+
return! asyncio.create_task(asyncio.sleep(0.1, 42))
35+
}
36+
let result = asyncio.run(tsk)
37+
result |> equal 42

0 commit comments

Comments
 (0)