Skip to content

Commit

Permalink
Prepare timeflies for the new asyncio mainloop (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrattli authored Oct 17, 2023
1 parent 422c785 commit 5735a63
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"fable": {
"version": "4.2.1",
"version": "4.2.2",
"commands": [
"fable"
]
Expand Down
23 changes: 6 additions & 17 deletions examples/timeflies/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open FSharp.Control
open Fable.Python
open Fable.Python.TkInter
open Fable.Python.Queue
open Fable.Python.AsyncIO

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

let agent = MailboxProcessor<TkInter.Event>.Start (workerAsync)

let frame = Frame(root, width = 800, height = 600, bg = "white")
frame.bind ("<Motion>", agent.Post) |> ignore
frame.pack ()
Expand All @@ -47,7 +47,7 @@ let stream =
let sink (ev: Notification<Label * int * int>) =
async {
match ev with
| OnNext (label, x, y) -> queue.put (Place(label, x, y))
| OnNext (label, x, y) -> label.place (x, y)
| OnError (err) -> printfn $"Stream Error: {err}"
| _ -> printfn "Stream Completed!"
}
Expand All @@ -56,22 +56,11 @@ let mainAsync =
async {
use! disposable = stream.SubscribeAsync(sink)

let rec update () =
let size = queue.qsize ()

for _ in 1..size do
let msg = queue.get (false)

match msg with
| Place (label, x, y) -> label.place (x, y)
| _ -> ()

match size with
| n when n > 0 -> root.after (1, update)
| _ -> root.after (10, update)
while true do
while root.dooneevent(int Flags.DONT_WAIT) do
()

root.after (1, update)
root.mainloop ()
do! Async.AwaitTask(asyncio.create_task(asyncio.sleep(0.005)))
}

[<EntryPoint>]
Expand Down
9 changes: 6 additions & 3 deletions examples/timeflies/TimeFlies.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<WarnOn>3390;$(WarnOn)</WarnOn>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Fable.Python" Version="4.0.0-theta-*" />
<PackageReference Include="Fable.Core" Version="4.0.0-theta-*" />
<PackageReference Include="Fable.Python" Version="4.*" />
<PackageReference Include="Fable.Core" Version="4.*" />
<PackageReference Include="FSharp.Control.AsyncRx" Version="1.6.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Fable.Python.fsproj" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion examples/timeflies/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Dag Brattli <[email protected]>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.9"
python = "^3.11"
fable-library = ">=0.8.0"
#fable-python = "*"
#fsharp-control-async-rx = "*"
Expand Down
4 changes: 4 additions & 0 deletions src/stdlib/TkInter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module Fable.Python.TkInter

open Fable.Core

type Flags =
| DONT_WAIT = 2

[<Import("Event", "tkinter")>]
type Event =
abstract member x: int
Expand Down Expand Up @@ -33,6 +36,7 @@ type Tk(screenName: string option) =
member _.update() = nativeOnly
member _.mainloop() = nativeOnly
member _.after(msecs: int, callback: unit -> unit) = nativeOnly
member _.dooneevent(flags: int) = nativeOnly

[<Import("Frame", "tkinter")>]
type Frame(master: Misc) =
Expand Down
10 changes: 9 additions & 1 deletion src/stdlib/asyncio/Tasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ open Fable.Core
type Task<'T> =
inherit Future<'T>

type Task =
inherit Future<unit>

type Coroutine<'T> =
inherit Awaitable<'T>

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

type IExports =
abstract create_task<'T> : fn: Coroutine<'T> -> Task<'T>
abstract sleep: seconds: float -> result: 'T -> Future<'T>
/// Translates a Python Task to a System.Threading.Tasks.Task
abstract create_task: task: Task -> System.Threading.Tasks.Task
/// Translates a Python Task<'T> to a System.Threading.Tasks.Task<'T>
abstract create_task: task: Task<'T> -> System.Threading.Tasks.Task<'T>
abstract sleep: seconds: float -> Task
abstract sleep: seconds: float * result: 'T -> Task<'T>
abstract run<'T> : main: Awaitable<'T> -> 'T
abstract run<'T> : main: System.Threading.Tasks.Task<'T> -> 'T

Expand Down
16 changes: 16 additions & 0 deletions test/TestAsyncIO.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,19 @@ let ``test builder run int works`` () =
let result = asyncio.run(tsk)
result |> equal 42

[<Fact>]
let ``test sleep works`` () =
let tsk = task {
do! asyncio.create_task(asyncio.sleep(0.1))
return 42
}
let result = asyncio.run(tsk)
result |> equal 42

[<Fact>]
let ``test sleep with value works`` () =
let tsk = task {
return! asyncio.create_task(asyncio.sleep(0.1, 42))
}
let result = asyncio.run(tsk)
result |> equal 42

0 comments on commit 5735a63

Please sign in to comment.